Forum: Ruby FixNum#times is correct?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Iñaki Baz C. (Guest)
on 2009-02-11 10:31
(Received via mailing list)
irb> 5.times {|i| puts i}
0
1
2
3
4
5


Is it the expected output? I was expecting:
0
1
2
3
4
Ben B. (Guest)
on 2009-02-11 10:33
(Received via mailing list)
On Wed, Feb 11, 2009 at 12:29 AM, Iñaki Baz C. 
<removed_email_address@domain.invalid> wrote:
> irb> 5.times {|i| puts i}
> 0
> 1
> 2
> 3
> 4
> 5

I think you may have clipped some of the output.  Here's what I get:

  irb(main):005:0> 5.times {|i| puts i}
  0
  1
  2
  3
  4
  => 5

It's puts'ing 0-4, and then returning 5.  To see this more clearly, try
this:

  5.times {|i| puts "-- #{i}" }

Ben
Stefano C. (Guest)
on 2009-02-11 10:35
(Received via mailing list)
Alle Wednesday 11 February 2009, Iñaki Baz C. ha scritto:
> 0
> 1
> 2
> 3
> 4

The 5 you see is the return value of 5.times, which irb displays as it
does
with the value returned by each expression you evaluate. If you try the
above
code from a script or from command line, you'll get the expected result:

ruby -e '5.times{|i| puts i}'

0
1
2
3
4

Stefano
Alexander S. (Guest)
on 2009-02-11 10:36
(Received via mailing list)
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Haha!

ruby 1.8.6 (2007-09-24 patchlevel 111) [i686-darwin9.2.0]
ruby 1.8.6: main
 >> 5.times {|i| puts i }
0
1
2
3
4
# => 5

11 февр. 2009, в 10:29, Iñaki Baz C. написал(а):

> 0
> 1
> 2
> 3
> 4
>
>
>
> --
> Iñaki Baz C.

- --
WBR, Alexander Simonov (DEVL-RIPE)
Developer, System A.
Web S.: http://simonov.in.ua
E-Mail: removed_email_address@domain.invalid




-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.9 (Darwin)

iEYEARECAAYFAkmSjdgACgkQkr9AYoS+FlX4eQCg6ZuRPBmcH+MxxqiM3T1yMdP7
L5EAnihKNvxYEURo4oTGK8T1UxKejttR
=EoOm
-----END PGP SIGNATURE-----
Iñaki Baz C. (Guest)
on 2009-02-11 10:42
(Received via mailing list)
El Miércoles, 11 de Febrero de 2009, Ben B.
escribió:> It's puts'ing 0-4, and then returning 5.  To see this more clearly, 
try
> this:
>
>   5.times {|i| puts "-- #{i}" }

Ops, my IRB configuration is not helping me :)

Thanks for the explanation.
This topic is locked and can not be replied to.