1 2 3
(1..10).each do |i| puts i end
Refactorings
No refactoring yet !
Gary Haran
September 18, 2007, September 18, 2007 10:26, permalink
definitely less keystrokes
1 2 3
for i in (1..10) puts i end
jamesgolick
September 19, 2007, September 19, 2007 13:02, permalink
or even shorter
1
(1..10).each { |i| puts i }
macournoyer
September 19, 2007, September 19, 2007 13:08, permalink
what about...
1
puts (1..10).to_a * "\n"
macournoyer
September 19, 2007, September 19, 2007 14:04, permalink
Ok now I doubt you could make this any shorter
1
puts (1..10).to_a
meebo
September 27, 2007, September 27, 2007 15:33, permalink
This also works, though it isn't shorter. It looks nice, though.
1
1.upto(10) { |i| puts i }
macournoyer
September 27, 2007, September 27, 2007 15:51, permalink
Right! Forgot about that one (upto). Definatly easier to read, nice one meebo!
Ari Brown
September 28, 2007, September 28, 2007 04:29, permalink
You could save that line into a separate file as a library, and then actually call it really easily!
1
load "x"
she
October 4, 2007, October 04, 2007 00:17, permalink
I think we have a winner... the
puts *(1..10)
!
but actually
p *(1..10)
works too :)
Ray D. Noper
October 5, 2007, October 05, 2007 05:25, permalink
AFAIK, you can lose () also ? Another 2 characters down...
1
p *1..10
Brian
December 17, 2007, December 17, 2007 19:21, permalink
Using range is really cool thing In ruby.
Also, .times is cool feature
Thanks,
Mona
December 20, 2007, December 20, 2007 06:58, permalink
Brian,
All ruby loops are cool and creative. Whether range or times or until. It is just great programming language.
Thanks
Nick Karnik
December 30, 2007, December 30, 2007 22:30, permalink
I had a good laugh at this thread ...LOL ... very impressive!
Dor Kalev
December 31, 2007, December 31, 2007 22:27, permalink
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
Dreamhost promo code
January 25, 2008, January 25, 2008 06:29, permalink
Using range is cool. I would also suggest using times. Like
10.times do |x|
puts x
end
Thanks
Chris Rhoades
August 18, 2008, August 18, 2008 22:30, permalink
for i in 1..10
puts i
end
2 less characters :) gotta save those bits mate.
Matthew Lafferty
August 18, 2008, August 18, 2008 22:31, permalink
for i in 1..10
puts i
end
cheaky!
me
September 10, 2008, September 10, 2008 17:45, permalink
1 2 3
class Integer; def p; puts self; end; end; (1..10).each &:p
Nicholas
November 27, 2008, November 27, 2008 10:54, permalink
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
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
Sakari
February 2, 2009, February 02, 2009 09:20, permalink
This is just stupid .. no need to ponder on code like this.
Daniel Rosenstark
February 6, 2009, February 06, 2009 15:45, permalink
@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]...
Longer
1 2 3
10.times do |x| puts x end
Shorter
1 2 3
10.times { |x| puts x }
Daniel Rosenstark
February 6, 2009, February 06, 2009 15:47, permalink
@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...
Longer With Do-Each
1 2 3
(1..10).each do |i| puts i end
Shorty with Braces
1 2 3
(1..10).each { |i| puts i }
Tj Holowaychuk
February 27, 2009, February 27, 2009 00:26, permalink
@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
1 2 3 4 5 6 7 8 9 10 11
class Foo def to_ary return 1,2,3,4,5 end end def bar *args p args end bar *Foo.new # => [1, 2, 3, 4, 5]
How to make this shorter ?