Bfec5f7d1a4aaafc5a2451be8c42d26a

How to make this shorter ?

1
2
3
(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

1
2
3
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
(1..10).each { |i| puts i }
Bfec5f7d1a4aaafc5a2451be8c42d26a

macournoyer

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

2 ratings. Login to rate!

what about...

1
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

1
puts (1..10).to_a
Avatar

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
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!
1
puts 1,2,3,4,5,6,7,8,9,10
Avatar

Dave

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

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

Vamsee

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

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

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!

1
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

1 rating. Login to rate!

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

1
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!

Avatar

me

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

No rating. Login to rate!
1
2
3
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!
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
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]...

Longer

1
2
3
10.times do |x|
   puts x
end

Shorter

1
2
3
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...

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

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

dreamhost

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

No rating. Login to rate!

very cool... thanks guys.

Your refactoring





Format Copy from initial code

or Cancel