Ruby: counters, counting and incrementing
Posted
by Shyam
on Stack Overflow
See other posts from Stack Overflow
or by Shyam
Published on 2010-04-14T23:36:13Z
Indexed on
2010/04/14
23:43 UTC
Read the original article
Hit count: 236
Hi,
If you have seen my previous questions, you'd already know I am a big nuby when it comes to Ruby. So, I discovered this website which is intended for C programming, but I thought whatever one can do in C, must be possible in Ruby (and more readable too).
The challenge is to print out a bunch of numbers. I discovered this nifty method .upto() and I used a block (and actually understanding its purpose). However, in IRb, I got some unexpected behavior.
class MyCounter
def run
1.upto(10) { |x| print x.to_s + " " }
end
end
irb(main):033:0> q = MyCounter.new
=> #<MyCounter:0x5dca0>
irb(main):034:0> q.run
1 2 3 4 5 6 7 8 9 10 => 1
I have no idea where the => 1 comes from :S Should I do this otherwise? I am expecting to have this result:
1 2 3 4 5 6 7 8 9 10
Thank you for your answers, comments and feedback!
© Stack Overflow or respective owner