Skip to content

Instantly share code, notes, and snippets.

@mjhea0
Created March 25, 2013 17:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mjhea0/5239056 to your computer and use it in GitHub Desktop.
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
# 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
@redsquirrel
Copy link

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. Use each when you want to iterate through something.

Use times when you want to do something a certain number of times.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment