<?xml version="1.0" encoding="UTF-8"?>
<code>
  <code>(1..10).each do |i|
  puts i
end</code>
  <comment>How to make this shorter ?</comment>
  <created-at type="datetime">2007-09-11T20:14:31+00:00</created-at>
  <id type="integer">2</id>
  <language>Ruby</language>
  <permalink>ruby-simple-loop</permalink>
  <refactors-count type="integer">32</refactors-count>
  <title>Ruby simple loop</title>
  <trackback-url></trackback-url>
  <updated-at type="datetime">2010-01-31T08:28:43+00:00</updated-at>
  <user-id type="integer">1</user-id>
  <refactors type="array">
    <refactor>
      <code>for i in (1..10)
  puts i
end</code>
      <code-id type="integer">2</code-id>
      <comment>definitely less keystrokes</comment>
      <created-at type="datetime">2007-09-18T10:26:43+00:00</created-at>
      <id type="integer">10</id>
      <language>Ruby</language>
      <rating type="integer">3</rating>
      <ratings-count type="integer">3</ratings-count>
      <title>On Ruby simple loop</title>
      <user-id type="integer">4</user-id>
      <user-name>Gary Haran</user-name>
      <user-website>http://www.garyharan.com/</user-website>
    </refactor>
    <refactor>
      <code>(1..10).each { |i| puts i }</code>
      <code-id type="integer">2</code-id>
      <comment>or even shorter</comment>
      <created-at type="datetime">2007-09-19T13:02:09+00:00</created-at>
      <id type="integer">14</id>
      <language>Ruby</language>
      <rating type="integer">3</rating>
      <ratings-count type="integer">2</ratings-count>
      <title>On Ruby simple loop</title>
      <user-id type="integer">2</user-id>
      <user-name>jamesgolick</user-name>
      <user-website>http://jamesgolick.com</user-website>
    </refactor>
    <refactor>
      <code>puts (1..10).to_a * "\n"</code>
      <code-id type="integer">2</code-id>
      <comment>what about...</comment>
      <created-at type="datetime">2007-09-19T13:08:52+00:00</created-at>
      <id type="integer">15</id>
      <language>Ruby</language>
      <rating type="integer">2</rating>
      <ratings-count type="integer">2</ratings-count>
      <title>On Ruby simple loop</title>
      <user-id type="integer">1</user-id>
      <user-name>macournoyer</user-name>
      <user-website>http://macournoyer.com</user-website>
    </refactor>
    <refactor>
      <code>puts (1..10).to_a</code>
      <code-id type="integer">2</code-id>
      <comment>Ok now I doubt you could make this any shorter</comment>
      <created-at type="datetime">2007-09-19T14:04:11+00:00</created-at>
      <id type="integer">20</id>
      <language>Ruby</language>
      <rating type="integer">3</rating>
      <ratings-count type="integer">3</ratings-count>
      <title>On Ruby simple loop</title>
      <user-id type="integer">1</user-id>
      <user-name>macournoyer</user-name>
      <user-website>http://macournoyer.com</user-website>
    </refactor>
    <refactor>
      <code>1.upto(10) { |i| puts i }</code>
      <code-id type="integer">2</code-id>
      <comment>This also works, though it isn't shorter. It looks nice, though.</comment>
      <created-at type="datetime">2007-09-27T15:33:29+00:00</created-at>
      <id type="integer">55</id>
      <language>Ruby</language>
      <rating type="integer">3</rating>
      <ratings-count type="integer">3</ratings-count>
      <title>On Ruby simple loop</title>
      <user-id type="integer" nil="true"></user-id>
      <user-name>meebo</user-name>
      <user-website></user-website>
    </refactor>
    <refactor>
      <code></code>
      <code-id type="integer">2</code-id>
      <comment>Right! Forgot about that one (upto). Definatly easier to read, nice one meebo!</comment>
      <created-at type="datetime">2007-09-27T15:51:45+00:00</created-at>
      <id type="integer">56</id>
      <language>Ruby</language>
      <rating type="integer">2</rating>
      <ratings-count type="integer">1</ratings-count>
      <title>On Ruby simple loop</title>
      <user-id type="integer">1</user-id>
      <user-name>macournoyer</user-name>
      <user-website>http://macournoyer.com</user-website>
    </refactor>
    <refactor>
      <code>puts 1,2,3,4,5,6,7,8,9,10</code>
      <code-id type="integer">2</code-id>
      <comment></comment>
      <created-at type="datetime">2007-09-27T21:35:27+00:00</created-at>
      <id type="integer">59</id>
      <language>Ruby</language>
      <rating type="integer">1</rating>
      <ratings-count type="integer">4</ratings-count>
      <title>On Ruby simple loop</title>
      <user-id type="integer" nil="true"></user-id>
      <user-name>Greg Buchholz</user-name>
      <user-website>http://kerneltrap.org/blog/6714</user-website>
    </refactor>
    <refactor>
      <code>puts *(1..10)</code>
      <code-id type="integer">2</code-id>
      <comment></comment>
      <created-at type="datetime">2007-09-28T00:05:47+00:00</created-at>
      <id type="integer">63</id>
      <language>Ruby</language>
      <rating type="integer">5</rating>
      <ratings-count type="integer">5</ratings-count>
      <title>On Ruby simple loop</title>
      <user-id type="integer" nil="true"></user-id>
      <user-name>Dave</user-name>
      <user-website></user-website>
    </refactor>
    <refactor>
      <code>10.times {|i| p i+1}</code>
      <code-id type="integer">2</code-id>
      <comment></comment>
      <created-at type="datetime">2007-09-28T03:44:58+00:00</created-at>
      <id type="integer">90</id>
      <language>Ruby</language>
      <rating type="integer">2</rating>
      <ratings-count type="integer">2</ratings-count>
      <title>On Ruby simple loop</title>
      <user-id type="integer" nil="true"></user-id>
      <user-name>Vamsee</user-name>
      <user-website></user-website>
    </refactor>
    <refactor>
      <code>load "x"</code>
      <code-id type="integer">2</code-id>
      <comment>You could save that line into a separate file as a library, and then actually call it really easily!</comment>
      <created-at type="datetime">2007-09-28T04:29:51+00:00</created-at>
      <id type="integer">96</id>
      <language>Ruby</language>
      <rating type="integer">1</rating>
      <ratings-count type="integer">3</ratings-count>
      <title>On Ruby simple loop</title>
      <user-id type="integer" nil="true"></user-id>
      <user-name>Ari Brown</user-name>
      <user-website></user-website>
    </refactor>
    <refactor>
      <code></code>
      <code-id type="integer">2</code-id>
      <comment>I think we have a winner... the 
puts *(1..10)
!


but actually
p *(1..10)

works too :)</comment>
      <created-at type="datetime">2007-10-04T00:17:38+00:00</created-at>
      <id type="integer">261</id>
      <language>Ruby</language>
      <rating type="integer">0</rating>
      <ratings-count type="integer">0</ratings-count>
      <title>On Ruby simple loop</title>
      <user-id type="integer" nil="true"></user-id>
      <user-name>she</user-name>
      <user-website></user-website>
    </refactor>
    <refactor>
      <code>p *1..10</code>
      <code-id type="integer">2</code-id>
      <comment>AFAIK, you can lose () also ? Another 2 characters down...
</comment>
      <created-at type="datetime">2007-10-05T05:25:11+00:00</created-at>
      <id type="integer">294</id>
      <language>Ruby</language>
      <rating type="integer">5</rating>
      <ratings-count type="integer">1</ratings-count>
      <title>On Ruby simple loop</title>
      <user-id type="integer" nil="true"></user-id>
      <user-name>Ray D. Noper</user-name>
      <user-website>http://sininepyhap2ev.blogspot.com</user-website>
    </refactor>
    <refactor>
      <code></code>
      <code-id type="integer">2</code-id>
      <comment>Using range is really cool thing In ruby.
Also, .times is cool feature
Thanks,
</comment>
      <created-at type="datetime">2007-12-17T19:21:27+00:00</created-at>
      <id type="integer">1178</id>
      <language>Ruby</language>
      <rating type="integer">0</rating>
      <ratings-count type="integer">0</ratings-count>
      <title>On Ruby simple loop</title>
      <user-id type="integer" nil="true"></user-id>
      <user-name>Brian</user-name>
      <user-website>http://www.floristone.com</user-website>
    </refactor>
    <refactor>
      <code></code>
      <code-id type="integer">2</code-id>
      <comment>Brian,
All ruby loops are cool and creative. Whether range or times or until. It is just great programming language.
Thanks</comment>
      <created-at type="datetime">2007-12-20T06:58:25+00:00</created-at>
      <id type="integer">1212</id>
      <language>Ruby</language>
      <rating type="integer">0</rating>
      <ratings-count type="integer">0</ratings-count>
      <title>On Ruby simple loop</title>
      <user-id type="integer" nil="true"></user-id>
      <user-name>Mona</user-name>
      <user-website>http://www.asaprepair.com</user-website>
    </refactor>
    <refactor>
      <code></code>
      <code-id type="integer">2</code-id>
      <comment>I had a good laugh at this thread ...LOL ... very impressive!</comment>
      <created-at type="datetime">2007-12-30T22:30:50+00:00</created-at>
      <id type="integer">1300</id>
      <language>Ruby</language>
      <rating type="integer">0</rating>
      <ratings-count type="integer">0</ratings-count>
      <title>On Ruby simple loop</title>
      <user-id type="integer" nil="true"></user-id>
      <user-name>Nick Karnik</user-name>
      <user-website>www.videomailz.com</user-website>
    </refactor>
    <refactor>
      <code></code>
      <code-id type="integer">2</code-id>
      <comment>I ran performance tests on all the options available here,
have a look:
http://www.dorkalev.com/2007/12/refactoring-we-are-not-alone.html</comment>
      <created-at type="datetime">2007-12-31T22:27:07+00:00</created-at>
      <id type="integer">1311</id>
      <language>Ruby</language>
      <rating type="integer">5</rating>
      <ratings-count type="integer">1</ratings-count>
      <title>On Ruby simple loop</title>
      <user-id type="integer" nil="true"></user-id>
      <user-name>Dor Kalev</user-name>
      <user-website>http://dorkalev.com</user-website>
    </refactor>
    <refactor>
      <code></code>
      <code-id type="integer">2</code-id>
      <comment>Using range is cool. I would also suggest using times. Like
10.times do |x|
puts x
end

Thanks</comment>
      <created-at type="datetime">2008-01-25T06:29:06+00:00</created-at>
      <id type="integer">1745</id>
      <language>Ruby</language>
      <rating type="integer">0</rating>
      <ratings-count type="integer">0</ratings-count>
      <title>On Ruby simple loop</title>
      <user-id type="integer" nil="true"></user-id>
      <user-name>Dreamhost promo code</user-name>
      <user-website>http://www.dreamhost-promo-code.net/</user-website>
    </refactor>
    <refactor>
      <code></code>
      <code-id type="integer">2</code-id>
      <comment>for i in 1..10
  puts i
end

2 less characters :)  gotta save those bits mate. </comment>
      <created-at type="datetime">2008-08-18T22:30:21+00:00</created-at>
      <id type="integer">15264</id>
      <language>Ruby</language>
      <rating type="integer">0</rating>
      <ratings-count type="integer">0</ratings-count>
      <title>On Ruby simple loop</title>
      <user-id type="integer" nil="true"></user-id>
      <user-name>Chris Rhoades</user-name>
      <user-website>http://www.wilkinsonskips.com</user-website>
    </refactor>
    <refactor>
      <code></code>
      <code-id type="integer">2</code-id>
      <comment>for i in 1..10 
puts i 
end


cheaky!</comment>
      <created-at type="datetime">2008-08-18T22:31:43+00:00</created-at>
      <id type="integer">15265</id>
      <language>Ruby</language>
      <rating type="integer">0</rating>
      <ratings-count type="integer">0</ratings-count>
      <title>On Ruby simple loop</title>
      <user-id type="integer" nil="true"></user-id>
      <user-name>Matthew Lafferty</user-name>
      <user-website>http://www.wilkinsonskips.co.uk</user-website>
    </refactor>
    <refactor>
      <code>class Integer; def p; puts self; end; end; 

(1..10).each &amp;:p</code>
      <code-id type="integer">2</code-id>
      <comment></comment>
      <created-at type="datetime">2008-09-10T17:45:21+00:00</created-at>
      <id type="integer">16948</id>
      <language>Ruby</language>
      <rating type="integer">0</rating>
      <ratings-count type="integer">0</ratings-count>
      <title>On Ruby simple loop</title>
      <user-id type="integer" nil="true"></user-id>
      <user-name>me</user-name>
      <user-website>http://1rad.wordpress.com</user-website>
    </refactor>
    <refactor>
      <code>name = 'nicholas'
(1..10).each { |i| puts i,name }

output

1 
nicholas
2
nicholas
3
nicholas
4
nicholas
5
nicholas
6
nicholas
7
nicholas
8
nicholas
9
nicholas
10
nicholas

</code>
      <code-id type="integer">2</code-id>
      <comment></comment>
      <created-at type="datetime">2008-11-27T10:54:44+00:00</created-at>
      <id type="integer">88479</id>
      <language>Ruby</language>
      <rating type="integer">0</rating>
      <ratings-count type="integer">0</ratings-count>
      <title>On Ruby simple loop</title>
      <user-id type="integer" nil="true"></user-id>
      <user-name>Nicholas</user-name>
      <user-website>www.lottosay.com</user-website>
    </refactor>
    <refactor>
      <code></code>
      <code-id type="integer">2</code-id>
      <comment>This is just stupid .. no need to ponder on code like this.</comment>
      <created-at type="datetime">2009-02-02T09:20:42+00:00</created-at>
      <id type="integer">145356</id>
      <language>Ruby</language>
      <rating type="integer">0</rating>
      <ratings-count type="integer">0</ratings-count>
      <title>On Ruby simple loop</title>
      <user-id type="integer" nil="true"></user-id>
      <user-name>Sakari</user-name>
      <user-website></user-website>
    </refactor>
    <refactor>
      <code>## Longer
10.times do |x|
   puts x
end

## Shorter
10.times { |x|
  puts x
}
</code>
      <code-id type="integer">2</code-id>
      <comment>@Sakari, I don't this is "stupid," if you're trying to learn Ruby (like me).

Anyway, I think that do end is the same as {}, so could be written (shorter)...

[I'm sure my code will be totally weird here]...




</comment>
      <created-at type="datetime">2009-02-06T15:45:19+00:00</created-at>
      <id type="integer">145981</id>
      <language>Ruby</language>
      <rating type="integer">0</rating>
      <ratings-count type="integer">0</ratings-count>
      <title>On Ruby simple loop</title>
      <user-id type="integer" nil="true"></user-id>
      <user-name>Daniel Rosenstark</user-name>
      <user-website>http://www.confusionists.com</user-website>
    </refactor>
    <refactor>
      <code>## Longer With Do-Each
(1..10).each do |i|
  puts i
end

## Shorty with Braces
(1..10).each { |i|
  puts i
}
</code>
      <code-id type="integer">2</code-id>
      <comment>@Sakari, I don't think this is "stupid," if you're trying to learn Ruby (like me).

Anyway, I think that do end is the same as {}, so the translation is like this...</comment>
      <created-at type="datetime">2009-02-06T15:47:54+00:00</created-at>
      <id type="integer">145982</id>
      <language>Ruby</language>
      <rating type="integer">0</rating>
      <ratings-count type="integer">0</ratings-count>
      <title>On Ruby simple loop</title>
      <user-id type="integer" nil="true"></user-id>
      <user-name>Daniel Rosenstark</user-name>
      <user-website>http://www.confusionists.com</user-website>
    </refactor>
    <refactor>
      <code>class Foo
  def to_ary
    return 1,2,3,4,5
  end
end

def bar *args
  p args
end

bar *Foo.new   # =&gt;  [1, 2, 3, 4, 5]
</code>
      <code-id type="integer">2</code-id>
      <comment>@Daniel Rosenstark yes do / end and {} are both closure literals when in the correct context. {} us typically used for one-liners where as do / end in multi-liners (not always the case though obviously) this is a pretty funny exercise, the range splat is cool though. I am not sure exactly of the C implementation but splat does call #to_ary so you can do things like this
</comment>
      <created-at type="datetime">2009-02-27T00:26:42+00:00</created-at>
      <id type="integer">148916</id>
      <language>Ruby</language>
      <rating type="integer">0</rating>
      <ratings-count type="integer">0</ratings-count>
      <title>On Ruby simple loop</title>
      <user-id type="integer">634</user-id>
      <user-name>Tj Holowaychuk</user-name>
      <user-website>http://vision-media.ca</user-website>
    </refactor>
    <refactor>
      <code></code>
      <code-id type="integer">2</code-id>
      <comment>very cool... thanks guys.</comment>
      <created-at type="datetime">2009-03-07T08:25:05+00:00</created-at>
      <id type="integer">149859</id>
      <language>Ruby</language>
      <rating type="integer">0</rating>
      <ratings-count type="integer">0</ratings-count>
      <title>On Ruby simple loop</title>
      <user-id type="integer" nil="true"></user-id>
      <user-name>dreamhost</user-name>
      <user-website>http://www.hosting-promo-code.net/</user-website>
    </refactor>
    <refactor>
      <code>if !nuts
then
 woman
else
 man</code>
      <code-id type="integer">2</code-id>
      <comment>This is my submission for cheaky code.</comment>
      <created-at type="datetime">2009-07-07T16:39:15+00:00</created-at>
      <id type="integer">191599</id>
      <language>Ruby</language>
      <rating type="integer">0</rating>
      <ratings-count type="integer">0</ratings-count>
      <title>On Ruby simple loop</title>
      <user-id type="integer" nil="true"></user-id>
      <user-name>dreamhost1</user-name>
      <user-website></user-website>
    </refactor>
    <refactor>
      <code></code>
      <code-id type="integer">2</code-id>
      <comment>I just wanted to say thanks. I recently started programming in ruby and this site has been tremendous help. Keep it up guys.</comment>
      <created-at type="datetime">2009-07-18T21:47:29+00:00</created-at>
      <id type="integer">205057</id>
      <language>Ruby</language>
      <rating type="integer">0</rating>
      <ratings-count type="integer">0</ratings-count>
      <title>On Ruby simple loop</title>
      <user-id type="integer" nil="true"></user-id>
      <user-name>dreamhost promo</user-name>
      <user-website>http://www.hosting-promo-code.net/</user-website>
    </refactor>
    <refactor>
      <code># Print your name in reverse X times.

printf("Enter a numer: ");
my_num = gets.strip.to_i;
printf("What's your name: ");
name = gets.strip;
my_num.times {|i|
    printf("%d.\t%s\n",i,name.reverse);
}</code>
      <code-id type="integer">2</code-id>
      <comment>I do enjoy the .times way best</comment>
      <created-at type="datetime">2009-08-29T09:08:07+00:00</created-at>
      <id type="integer">282613</id>
      <language>Ruby</language>
      <rating type="integer">0</rating>
      <ratings-count type="integer">0</ratings-count>
      <title>On Ruby simple loop</title>
      <user-id type="integer" nil="true"></user-id>
      <user-name>Brad</user-name>
      <user-website></user-website>
    </refactor>
    <refactor>
      <code>puts  [*1..10]
</code>
      <code-id type="integer">2</code-id>
      <comment>A little modification to winner. From Range to Array.</comment>
      <created-at type="datetime">2009-11-02T07:28:26+00:00</created-at>
      <id type="integer">352443</id>
      <language>Ruby</language>
      <rating type="integer">0</rating>
      <ratings-count type="integer">0</ratings-count>
      <title>On Ruby simple loop</title>
      <user-id type="integer" nil="true"></user-id>
      <user-name>Andrew</user-name>
      <user-website></user-website>
    </refactor>
    <refactor>
      <code>:questionNum.times do |x|
  puts x
end

for i in (1..@questionNum)
  puts i
end 
</code>
      <code-id type="integer">2</code-id>
      <comment>I'm learning Ruby/Haml and this is useful to learn.
I'm actually looking for a way to do the same thing taking the number from a symbol variable. Something like the code below, but any version doesn't work.
How could I better make it work?</comment>
      <created-at type="datetime">2010-01-30T00:44:08+00:00</created-at>
      <id type="integer">430547</id>
      <language>Ruby</language>
      <rating type="integer">0</rating>
      <ratings-count type="integer">0</ratings-count>
      <title>On Ruby simple loop</title>
      <user-id type="integer" nil="true"></user-id>
      <user-name>Granito Silestone</user-name>
      <user-website>http://xmariachi.blogspot.com/2009/12/granito-compac-silestone-comparacion.html</user-website>
    </refactor>
    <refactor>
      <code>p(*1..10)</code>
      <code-id type="integer">2</code-id>
      <comment>?</comment>
      <created-at type="datetime">2010-01-31T08:28:42+00:00</created-at>
      <id type="integer">431880</id>
      <language>Ruby</language>
      <rating type="integer">0</rating>
      <ratings-count type="integer">0</ratings-count>
      <title>On Ruby simple loop</title>
      <user-id type="integer" nil="true"></user-id>
      <user-name>tknv</user-name>
      <user-website></user-website>
    </refactor>
  </refactors>
</code>
