5. Welcome!
Welcome to ACM init(), a Computer Science class for
beginners.
Over the course of the next few weeks, you will be
learning Ruby, a flexible, widely used programming
language.
6. About the Course
• This course assumes no prior programming
experience
• Hosted by the Association for Computing
Machinery (ACM)
• This course will be teaching you how to code in
a language called Ruby
• NO EXAMS!!!!
7.
8. Resources for You
Our website: acm.cs.ucla.edu
Slides: slideshare.net/uclaacm
Facebook: https://www.facebook.com/groups/717821374976381/
Want to contact us?
Kelly: omalleyk@ucla.edu
Daniel: dduan@yahoo.com
Akshay: akshaybakshi@ucla.edu
9. Remember: If you
have any questions,
ask! Chances are
someone else has the
same question and is
hesitant to ask.
10. If you don’t
understand
something, it is more
important that you
learn it than we finish
everything today.
11. Why Computer Science?
• Computer Science is a fast
growing field with a surplus of
high-paying jobs
• Learning a programming
language will enhance your
resume and make you more
desirable to companies
• Furthermore, learning how to
code will better your problem-solving
skills and help you in
all of your classes
Source: http://www.ovrdrv.com/blog/making-computer-science-cool/
14. Why Learn Ruby?
• Ruby is easy to read and use,
and is considered to be one of
the best first languages to
learn
• Ruby developers are in high
demand
• Ruby is useful -- Ruby on Rails
is one of the most popular
web programming platforms in
the startup community
16. Computing the Factorial of a
Number
• Factorial: written as #!
• For example, 5! is 5*4*3*2*1 = 120
• I wrote a simple program called fact.rb that will
compute the factorial for us
• Let's try it out! Compute the factorial of 6.
• ruby fact.rb 6
17.
18. A Bit About Computers
Computers think in 0s and 1s. Everything that you input into a
computer gets stored in this manner. For example:
You don't need to know how this conversion works. However, the concept is
important because it is the reason why we need programming languages.
Human-readable programming languages go through a process that
converts them to binary, which can be understood by the compter.
19. How do we make our code
work?
Ruby is what is called an interpreted language. That means
that the computer runs one line of code at a time.
We can't just type code into a word document and hope it will
magically do something. We need an interpreter that will help
the computer understand our code.
32. Data Types
• Why? Say "five". Did you say 5 or five?
• There are three main data types in Ruby: numbers,
booleans, and strings.
• Numbers: Exactly what it sounds like. (example: 1,
400, 45.7)
• Booleans: Something that is either true or false.
• String: A word or phrase. (example: "Hello World!")
33. Variables
We can store these data types in variables so we can
come back and use them again later.
Take a look:
Now if we want to use any of these stored values
again all we have to do is type the variable name!
34. Math
There are six main math operations we are going to go over
today.
35. + - * /
• + is addition
• - is subtraction
• * is multiplication
• / is division
• These work the same way
your calculator does -- no
surprises here
36. ** and %
• ** (exponent) raises one
number to the power of
another. For example, 2**3
would be 8 because 2*2*2=8
• % (modulus) returns the
remainder of division. For
example, 25%7 would be 4
because 25/7 is 3r4.
37. Printing to the Screen
There are two main commands that will allow you to
print text out to the screen.
These commands are 'puts' and 'print'.
38. 'puts' vs 'print'
The 'print' command just takes whatever you give it and prints
it to the screen.
'puts' (for "put string") is slightly different: it adds a new (blank)
line after the thing you want it to print.
40. String Methods
• Remember, a string is is word or a phrase. Ruby
provides several methods for us to make it easier to
use strings
• '.length' : finds the length of the string
• '.reverse' : reverses the string
• '.upcase' : changes all letters to uppercase
• '.downcase' : changes all letters to lowercase
42. We've covered all the Ruby syntax
we're going over for today. Now,
let's learn a bit about writing good
code.
43. Comments
What if you write a really long program then not look at it for a
year? What if you're working on a project with other people
and you have to share your code?
We need a way to explain what is going on in the programs we
write.
The solution? Comments.
44. How to add comments
Comments are a tool we have to communicate what our code
is intended to do.
To put a comment in your code all you have to do is type
# your comment here
Anything that comes after '#' and is on the same line will be
ignored when the code is being run.
46. Naming Conventions
• Remember that we can give values to variables so we can use the same
value again later
• We can name these variables whatever we want
• Doesn't it make sense to give variables descriptive names?
• Some naming rules:
• Separate words with underscores '_'
• Do not start a variable name with a number, symbol, or uppercase
letter
• The variable name should describe what the variable contains or will
be used for
48. What we covered
• A little bit about Ruby
• How to use Koding
• Data types: numbers, string, and booleans
• Variables
• Math
• How to print to the screen
• String methods
• How to write good code