Created
March 25, 2013 17:47
-
-
Save mjhea0/5239056 to your computer and use it in GitHub Desktop.
Ruby feels like Perl in that there's a TON of different ways to do one thing. Bloated API? Theme music: Ween - So many people in the neighborhood
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# loop | |
x = 0 | |
loop do | |
x += 1 | |
puts x | |
break if x > 4 | |
end | |
puts | |
# while | |
i = 0 | |
while i < 5 | |
i += 1 | |
puts i | |
end | |
puts | |
# until | |
j = 0 | |
until j > 4 | |
j += 1 | |
puts j | |
end | |
puts | |
# for | |
num = (1..5) | |
for n in num | |
puts n | |
end | |
puts | |
# times | |
t = 1 | |
5.times do | |
puts t | |
t += 1 | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Use
loop
when you want an infinite loop.Use
while
when you want to keep checking something for a state change.Don't use
until
. Just don't.Don't use
for
. Useeach
when you want to iterate through something.Use
times
when you want to do something a certain number of times.