On 11/06/2013 05:58 PM, Sur wrote:
> @Stefan
>
> It's nothing like that Ruby is forgetting the loop's local variables or
> so...
>
> To me, the code has some problems. We are actually initializing the "j"
> in every repetition of the loop and that's why it is not printing
> anything for a nil object.
> What we are doing above is exactly like this:
>
> Ruby 1.9.3
>
> a = 1
> print a # => prints 1 and returns nil
> a = nil
> print a # => prints blank and returns nil
>
> Ruby 1.8.7
>
> a = 1
> print a # => prints 1 and returns nil
> a = nil
> print a # => prints "nil" and returns nil
>
> So, only difference in Ruby 1.8 and 1.9.3 is that earlier it used to
> print "nil" for nil objects and now it doesn't print anything for nil
> objects which logically sounds better.
>
> In your code if you'd remove the initialization of "j" in every rep of
> the loop you'll get what you want:
>
> e.g.
>
> Ruby 1.9.3
>
> 5.times{|i| if i == 0 then j = 1 end; print i, j,"\n"}
> 01
> 11
> 21
> 31
> 41
>   => 5
>
> /Note that the block variables now is only |i|  and not |i; j|/
>
>
>
> regards,
> Sur
> crismon9.com <http://crismon9.com>


This is not accurate.

1.8.7 :001 > 5.times{|i| if i == 0 then j = 1 end; print i, j,"\n"}
01
1nil
2nil
3nil
4nil
  => 5

1.9.3p448 :001 > 5.times{|i| if i == 0 then j = 1 end; print i, j,"\n"}
01
1
2
3
4
  => 5

-Justin