Skip to content

Instantly share code, notes, and snippets.

@hungneox
Last active August 29, 2015 13:58
Show Gist options
  • Save hungneox/10000674 to your computer and use it in GitHub Desktop.
Save hungneox/10000674 to your computer and use it in GitHub Desktop.
Loops in Ruby
for num in 1..10
puts num
end
for i in 1..50
print i
end
i = 1
while i <= 50 do
print i
i+=1
end
i = 1
until i > 50 do
print i
i += 1
end
i = 0
loop do
print "Ruby!"
i += 1
break if i == 30
end
i = 20
loop do
i -= 1
next if i % 2 != 0
print "#{i}"
break if i <= 0
end
array = [1,2,3,4,5]
array.each do |x|
x += 10
print "#{x}\n"
end
object.each { |item| # Do something }
object.each do |item| # Do something end
odds = [1,3,5,7,9]
odds.each do |item|
puts item
end
#print Chunky bacon 10 times
10.times { print "Chunky bacon!"}
30.times { print "Ruby!" }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment