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!
How to make this shorter ?