(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
for i in (1..10) puts i end
jamesgolick
September 19, 2007, September 19, 2007 13:02, permalink
or even shorter
(1..10).each { |i| puts i }
macournoyer
September 19, 2007, September 19, 2007 13:08, permalink
what about...
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
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.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!
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...
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
class Integer; def p; puts self; end; end; (1..10).each &:p
Nicholas
November 27, 2008, November 27, 2008 10:54, permalink
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]...
10.times do |x| puts x end
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...
(1..10).each do |i| puts i end
(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
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]
dreamhost1
July 7, 2009, July 07, 2009 16:39, permalink
This is my submission for cheaky code.
if !nuts then woman else man
dreamhost promo
July 18, 2009, July 18, 2009 21:47, permalink
I just wanted to say thanks. I recently started programming in ruby and this site has been tremendous help. Keep it up guys.
Brad
August 29, 2009, August 29, 2009 09:08, permalink
I do enjoy the .times way best
# 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);
}
Andrew
November 2, 2009, November 02, 2009 07:28, permalink
A little modification to winner. From Range to Array.
puts [*1..10]
Granito Silestone
January 30, 2010, January 30, 2010 00:44, permalink
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?
:questionNum.times do |x| puts x end for i in (1..@questionNum) puts i end
kinofrost
August 23, 2011, August 23, 2011 03:38, permalink
It should be noted that "puts" and "p" are not equivalent, so it should only refactor using "puts"; so this is the shortest:
puts *1..10
kinofrost
August 23, 2011, August 23, 2011 03:39, permalink
It should be noted that "puts" and "p" are not equivalent, so it should only refactor using "puts"; so this is the shortest:
puts *1..10
dreamhostpromo
August 27, 2011, August 27, 2011 00:45, permalink
LESS50
This coupon code is give you 50 $ off for new customer of dreamhost and also get 15 days free trial
dreamhostpromo
August 27, 2011, August 27, 2011 00:45, permalink
LESS50
This coupon code is give you 50 $ off for new customer of dreamhost and also get 15 days free trial
dreamhostpromo
August 27, 2011, August 27, 2011 00:46, permalink
LESS50
This coupon code is give you 50 $ off for new customer of dreamhost and also get 15 days free trial
<p>LESS50 This coupon code is give you 50 $ off for new customer of dreamhost and also get 15 days free trial.</p>
How to make this shorter ?