Bfec5f7d1a4aaafc5a2451be8c42d26a

How to make this shorter ?

(1..10).each do |i|
  puts i
end

Refactorings

No refactoring yet !

403e57e2be130d2218f992b86dfa8260

Gary Haran

September 18, 2007, September 18, 2007 10:26, permalink

3 ratings. Login to rate!

definitely less keystrokes

for i in (1..10)
  puts i
end
F6eddf2f983d23c2d031e407852625e9

jamesgolick

September 19, 2007, September 19, 2007 13:02, permalink

2 ratings. Login to rate!

or even shorter

(1..10).each { |i| puts i }
Bfec5f7d1a4aaafc5a2451be8c42d26a

macournoyer

September 19, 2007, September 19, 2007 13:08, permalink

2 ratings. Login to rate!

what about...

puts (1..10).to_a * "\n"
Bfec5f7d1a4aaafc5a2451be8c42d26a

macournoyer

September 19, 2007, September 19, 2007 14:04, permalink

3 ratings. Login to rate!

Ok now I doubt you could make this any shorter

puts (1..10).to_a
D41d8cd98f00b204e9800998ecf8427e

meebo

September 27, 2007, September 27, 2007 15:33, permalink

3 ratings. Login to rate!

This also works, though it isn't shorter. It looks nice, though.

1.upto(10) { |i| puts i }
Bfec5f7d1a4aaafc5a2451be8c42d26a

macournoyer

September 27, 2007, September 27, 2007 15:51, permalink

1 rating. Login to rate!

Right! Forgot about that one (upto). Definatly easier to read, nice one meebo!

F50171eef7a58cdab2c70497fa6ba947

Greg Buchholz

September 27, 2007, September 27, 2007 21:35, permalink

4 ratings. Login to rate!
puts 1,2,3,4,5,6,7,8,9,10
D41d8cd98f00b204e9800998ecf8427e

Dave

September 28, 2007, September 28, 2007 00:05, permalink

5 ratings. Login to rate!
puts *(1..10)
22100344f91ad34ad9fb8fb6b6940a84

Vamsee

September 28, 2007, September 28, 2007 03:44, permalink

2 ratings. Login to rate!
10.times {|i| p i+1}
D41d8cd98f00b204e9800998ecf8427e

Ari Brown

September 28, 2007, September 28, 2007 04:29, permalink

3 ratings. Login to rate!

You could save that line into a separate file as a library, and then actually call it really easily!

load "x"
F8252f6b892312c3f56cbc300360f064

she

October 4, 2007, October 04, 2007 00:17, permalink

No rating. Login to rate!

I think we have a winner... the
puts *(1..10)
!

but actually
p *(1..10)

works too :)

692f9ff0f54f04b92f60638fa3641daf

Ray D. Noper

October 5, 2007, October 05, 2007 05:25, permalink

2 ratings. Login to rate!

AFAIK, you can lose () also ? Another 2 characters down...

p *1..10
38e32b46b7e1ccc16821d3c48cdb4764

Brian

December 17, 2007, December 17, 2007 19:21, permalink

No rating. Login to rate!

Using range is really cool thing In ruby.
Also, .times is cool feature
Thanks,

28dde1b556f26610aac9b328dc4bc234

Mona

December 20, 2007, December 20, 2007 06:58, permalink

No rating. Login to rate!

Brian,
All ruby loops are cool and creative. Whether range or times or until. It is just great programming language.
Thanks

38857271669e9b52fc652ab95f05b6f9

Nick Karnik

December 30, 2007, December 30, 2007 22:30, permalink

No rating. Login to rate!

I had a good laugh at this thread ...LOL ... very impressive!

0a5eb48929f4014d1a1bf448517158d5

Dor Kalev

December 31, 2007, December 31, 2007 22:27, permalink

1 rating. Login to rate!

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

4c7bcd695bde98877d8b5851e7676bd0

Dreamhost promo code

January 25, 2008, January 25, 2008 06:29, permalink

No rating. Login to rate!

Using range is cool. I would also suggest using times. Like
10.times do |x|
puts x
end

Thanks

40f54460701ba275712b08ca3642ba04

Chris Rhoades

August 18, 2008, August 18, 2008 22:30, permalink

No rating. Login to rate!

for i in 1..10
puts i
end

2 less characters :) gotta save those bits mate.

7ef4a8b5dacedba4e77d601ce648d098

Matthew Lafferty

August 18, 2008, August 18, 2008 22:31, permalink

No rating. Login to rate!

for i in 1..10
puts i
end

cheaky!

D41d8cd98f00b204e9800998ecf8427e

me

September 10, 2008, September 10, 2008 17:45, permalink

No rating. Login to rate!
class Integer; def p; puts self; end; end; 

(1..10).each &:p
88d5b5bbfe4d0f1b3eee552d7852b3c6

Nicholas

November 27, 2008, November 27, 2008 10:54, permalink

No rating. Login to rate!
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
5b753d3dc81598c1facc761558fc00a3

Sakari

February 2, 2009, February 02, 2009 09:20, permalink

No rating. Login to rate!

This is just stupid .. no need to ponder on code like this.

C7e5e284f6a92e5b293771f46e849314

Daniel Rosenstark

February 6, 2009, February 06, 2009 15:45, permalink

No rating. Login to rate!

@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
}
C7e5e284f6a92e5b293771f46e849314

Daniel Rosenstark

February 6, 2009, February 06, 2009 15:47, permalink

No rating. Login to rate!

@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
}
F1e3ab214a976a39cfd713bc93deb10f

Tj Holowaychuk

February 27, 2009, February 27, 2009 00:26, permalink

No rating. Login to rate!

@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]
4fb47b08a0f3d762374d7549d3d5b6c5

dreamhost

March 7, 2009, March 07, 2009 08:25, permalink

No rating. Login to rate!

very cool... thanks guys.

B4a1c3041f7c501db2f25505f59c050c

dreamhost1

July 7, 2009, July 07, 2009 16:39, permalink

No rating. Login to rate!

This is my submission for cheaky code.

if !nuts
then
 woman
else
 man
3749c177ceef625324d130d32046ef3e

dreamhost promo

July 18, 2009, July 18, 2009 21:47, permalink

No rating. Login to rate!

I just wanted to say thanks. I recently started programming in ruby and this site has been tremendous help. Keep it up guys.

F53a59cf5cd475ddfb08b865a0ae1256

Brad

August 29, 2009, August 29, 2009 09:08, permalink

No rating. Login to rate!

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);
}
D41d8cd98f00b204e9800998ecf8427e

Andrew

November 2, 2009, November 02, 2009 07:28, permalink

No rating. Login to rate!

A little modification to winner. From Range to Array.

puts  [*1..10]
Ed00ebbecd51d32f258876e55d894c9b

Granito Silestone

January 30, 2010, January 30, 2010 00:44, permalink

No rating. Login to rate!

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
08c1e816bc0e671068d03b8a34aa7c27

tknv

January 31, 2010, January 31, 2010 08:28, permalink

No rating. Login to rate!

?

p(*1..10)
D41d8cd98f00b204e9800998ecf8427e

duffy.jp

December 22, 2010, December 22, 2010 17:58, permalink

No rating. Login to rate!

One character less:

p *1..10
Ba18a7993e58bea24288629332d21c41

kinofrost

August 23, 2011, August 23, 2011 03:38, permalink

No rating. Login to rate!

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
Ba18a7993e58bea24288629332d21c41

kinofrost

August 23, 2011, August 23, 2011 03:39, permalink

No rating. Login to rate!

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
C25553faf4ec549d80a2d75428f0e038

dreamhostpromo

August 27, 2011, August 27, 2011 00:45, permalink

No rating. Login to rate!

LESS50
This coupon code is give you 50 $ off for new customer of dreamhost and also get 15 days free trial

C25553faf4ec549d80a2d75428f0e038

dreamhostpromo

August 27, 2011, August 27, 2011 00:45, permalink

No rating. Login to rate!

LESS50
This coupon code is give you 50 $ off for new customer of dreamhost and also get 15 days free trial

C25553faf4ec549d80a2d75428f0e038

dreamhostpromo

August 27, 2011, August 27, 2011 00:46, permalink

No rating. Login to rate!

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>

Your refactoring





Format Copy from initial code

or Cancel