Ruby in 100 Minutes
04 Apr 2014####string gotcha
Ruby can’t do string + num
Use .to_s
to get around it
Or maybe use this:
####Symbols
- Starts with a colon then one or more letters
- Less methods than Strings
####Iterating
Use times
method to repeat an instruction
####Arrays
- Return
nil
when index out of range <<
operator for appending a signle element- Elements don’t need to be of same type
####Hashes
{ key => value ... }
- methods:
keys
,values
-
simplified syntax when all keys are symbols:
{ symbol: value ... }
(for Ruby 1.9 and higher)
####Conditionals
method name ending in a ?
: return true
or false
####chomp
- get rid of the
Enter
gets.chomp
####exponentiation
**
5**2
####random
- rand(1) always produce 0
- rand(101): 0-100
- srand
- srand 0 seed with current time in ms
####block as a parameter
- precede the parameter with an ampersand(&)
####global variable
- preced the variable with $
Your Comments
comments powered by Disqus