>> x = 6
?> if x > 3 then
?> puts "greater!!"
>> end
greater!!
# else
>> x = 2
?> if x > 3 then
?> puts "greater!!"
?> else
?> puts "lower!!"
>> end
lower!!
>> puts "greater!!" if x > 1
greater!!
# Opposite of 'if' statement
>> x = 5
=> unless x == 10 then print "#{x}" end
=> print "#{x}" unless x == 10
=> x = 5
>> case x
| when 1 print "one"
| when 2 print "two"
| when 3 print "three"
| else print "no idea"
| end
# Assign the return value to a variable
>> id = 2000
>> name = case
| when id == 1111 then "Mark"
| when id == 1112 then "Bob"
| when id >= 2000 then "Other"
>> id
=> 2000
>> name
=> Other
(1..10) === 4 # Use triple equal sign
name = "Bob"
name == "Bob" ? "Hi Bob" : "Who are you?"
Loops
# Repeats a block of code until the test expression is evaluated to false
>> i = 0
?> while i < 5 do
?> print i = i + 1,"\s"
>> end
1 2 3 4 5
=> nil
>> i = 0
>> print i = i + 1,"\s" while i < 5
1 2 3 4 5
# Repeats a block of code until the test expression is evaluated to true
>> i = 5
?> until i == 0
?> print i-=1,"\s"
>> end
4 3 2 1 0 => nil
>> i = 5
>> print i-=1,"\s" until i == 0
4 3 2 1 0 => nil
# iterates through the elements of an enumerable object
?> for i in [10,20,4] do
?> print i,"\s"
>> end
10 20 4 => [10, 20, 4]
>> hash = {:name => "fer", :gender => "maple" }
?> for key, value in hash
?> print "#{key}:#{value}\n"
>> end
name:fer
gender:maple
?> for i in 1..10
?> print "#{i}\s"
>> end
1 2 3 4 5 6 7 8 9 10 => 1..10
Iterators & Enumerators
>> (1..5).each {|i| print "#{i}\s" }
1 2 3 4 5 => 1..5
# Blocks!
?> ('a'..'h').each do |c|
?> print "#{c}\n"
>> end
a
b
c
d
e
f
g
h
=> "a".."h"
Enumerator: object whose purpose is to enumerate another enumerable object. This means that enumerators are enumerable too.
If you have a method that uses an enumerable object, you may not want to pass the enumerable collection object because it is mutable and the method may modify it.
So you can pass an enumerator created with to_enum and nothing will happen to the original enumerable collection
>> for i in 1..10
| print i,"\s"
| break if i == 5
| end
=> 1 2 3 4 5 => nil
>> (1..10).each do |i|
| print i,"\s"
| break if i == 5
| end
=> 1 2 3 4 5 => nil
# The next statement ends the current iteration and jumps to the next one.
>> for i in 1..10
| print i,"\s"
| next if i == 5
| end
=> 1 2 3 4 6 7 8 9 10 => nil
# Redo restarts the current iteration from th efirst instruction in the body
# of the loop or iteration
>> sum = 0
=> 0
>> for in i in 1..3
| sum +=1
| redo if sum == 1
| end
=> 1..3
>> sum
=> 7
>> 1+2+3
=> 6
BEGIN/END
These are two reserved words in Ruby.
# BEGIN allows the execution of Ruby code at the beginning of a Ruby program
# END allow sthe execution if Ruby code at the end of a Ruby program
BEGIN {
puts "Beginning"
}
END {
puts "Ending"
}
puts "Normal control flow"
=>
Beginning
Normal control flow
Ending
BEGIN {puts 1}
BEGIN {puts 2}
BEGIN {puts 3}
END {puts 4}
END {puts 5}
END {puts 6}
=>
1
2
3
6
5
4