Section Topics:
- Data Types: Numbers, Booleans, Strings
- Variables
- Math
- puts and print
There are 3 Data Types: Numbers, Booleans, and Strings
Booleans - Are either set equal to True or False.
Strings - This is any word(s) or number(s) in quotations. Quotes can be written in either ( ' or " ) form. Anything written written with quotes around them Ruby will take as a string.
Update (May 14, 2013): Ruby will error if you mix and match the quotation marks for a string.
Example:
- Input: print 'hello"
Output: error
Variables
A person sets a variable by stating what they want to call the variable and assigning it a value. Code academy gives me a number and it doesn't seem like Ruby will take anything other than numbers.
Ruby also does not take spaces in variables so underscores "_" are used instead.
Examples:
- cats = 3
dogs = 4
Input: cats + dogs
Output: 7 - my_num = 100
Input: my_num
Output: 100 - my num = 100
Input: my num
Output: error
my_num = 100
my_num=100
my_num= 100
my_num =100
Math
This is fairly straight forward. There are 6 operators:
- Addition (+)
- Subtraction (-)
- Multiplication (*)
- Division (/)
- Exponentiation (**)
- Modulo (%)
Example in Ruby:
- Input: 2**3
Output: 8
Example in Ruby:
- Input: 10%7
Output: 3
Update (May 14, 2013): Testing the division operator it seems that Ruby will only return whole numbers unless a you add a value in the tenth place, just adding a decimal will not do. As you can tell, without knowing this a person's calculations can be off by quite a bit.
Examples:
- Input: 3/2
Output: 1 - Input: 3./2
Output: 1 - Input: 3.0/2
Output: 1.5
puts and print
Now there's a reason why puts and print are undercased. Ruby is case sensitive and as far as I can tell if you capitalize them Ruby will return an error.
puts and print are both Ruby commands that outputs a string.
Example:
- Input: puts "hello"
print "good bye"
Output: hello
good bye
What is the difference between puts and print? I struggled with this the first time even with Code Academy's explaination. puts adds a blank line after the string while print does not.
Example:
- Input: print "hello"
print "good bye"
Output: hellogood bye - Input: puts "hello"
puts "good bye"
Output: hello
good bye
In addition is seems that if you can't put more than 1 command on a line. I guess that's why they call it a command line instead of a command(s) line. hahaha! (lame I know).
No comments:
Post a Comment