Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

A simple loop in Ruby (See related posts)

This loop repeats 10 times, with each iteration it increments the variable 'i' by 1 and displays it's value.

i = 0
10.times do
  i += 1
  puts i
end

Comments on this post

onkis posts on Dec 22, 2007 at 15:49
Or
0.upto(10) do |i|
    puts i
end
jrobertson posts on Dec 22, 2007 at 17:51
thanks onkis, that's the one I wanted but hadn't used it before. I could simplify it even more with just 1 line of code
0.upto(10) { |i| puts i}
jonuts posts on Jan 10, 2008 at 04:31
or..

(1..10).each{|i| puts i}
peter posts on Feb 21, 2008 at 13:33
Or even:

10.times do |i|
puts i
end

You need to create an account or log in to post comments to this site.


Click here to browse all 5147 code snippets

Related Posts