Ruby
Sources & Cool stuff
Naming conventions
a_variable
Variables
$global_variables
@@class_variables
I/O
puts
- inserts a
\n
- inserts a
print
gets
.chomp
to trim separators
Conditionals & Flow
The only false
values are:
nil
false
Everything else is true
So:
- "" is
true
- 0 is
true
For boolean evaluation there are classic operators and the spaceship operator:
<=>
returns- -1 if the value on the left is less than the value on the right
- 0 if … left is equal to … right
- 1 if … left is greater than … right
Used for sorting.
For flow control there are:
-
if … else
-
elifs
-
case … when … then
-
unless … else
-
cond ? if_true : if_false
-
Loops > Article on Skorks About Loops and Iterators
while
for
loop
until
.times
.upto
.downto
-
Arrays > docs API > ZetCode tutorial
.last(n)
.first(n)
.push
.pop
<<
- shovel operator, like
push
- shovel operator, like
.shift
- removes the first element and returns
.unshift
- add elements at the beginning
.concat()
- works the same as +, also - can subtract any element from an array
To get a list of available methods run:
num_array.methods
-
Hashes Similar to JS' objects and Python’s dictionaries. Hashes are similar to arrays but in place of indexes to access the values stored it uses
keys
. Hashes depend solely an keys whereas arrays are highly dependant an order.hash = { "score" => 11, "the array" => [1, 2, 3] } # Symbol's concise syntax hash2 = { symbol1: "hello", symbol2: "world" } hash2[:symbol1] another_hash = Hash.new hash["score"] #=> 11
Using symbols instead of strings as keys is more efficient and readable.
.fetch(key, [default value])
- instead of silently returning
nil
it raises an error if the key is not in the hash
- instead of silently returning
.delete(key)
- also returns the value of the key-value pair
.merge(second_hash)