Ruby Cheat sheet
Simple examples of common ruby code
Flow
IF
if s == 'foo'
print 'foo'
elsif s == 'bar'
print 'bar'
else
print 'baz'
end
Unless
unless s == 'foo'
print 's is not foo'
else
print 's is foo'
end
Case
case S
when 'foo', 'goo'
print 'was foo or goo'
when 'moo'
print 'was moo'
else
print 'was something weird'
end
Loops
for i in 0..3
print "i is #{i}"
end
Normal While condition
while i < 3
print "i is #{i}"
end
While modifier
begin
print "i is #{i}"
i = i + 1
end while i < 3
Until
until i > 3
print "i is #{i}"
end
Until with modifier
begin
print "i is #{i}"
end untill i > 3
Each
(0...3).each do |i|
print "i is #{i}"
end
Messing with loops
next
-- skip the next loop iteration
redo
-- run the loop again without checking the loop condition.
break
-- exits a loop immediatly