From: Paul Smith Date: 2009-12-06T07:10:33+09:00 Subject: Re: different output for same expression? 2009/12/5 Jesús Gabriel y Galán : > On Sat, Dec 5, 2009 at 6:43 PM, Paul Smith wrote: >> On Sat, Dec 5, 2009 at 5:14 PM, Steven Arnold >> wrote: >>> Forgive me if this is an elementary question and I am missing something obvious, but there is a behavior in Ruby that puzzles me a lot. >>> >>> I don't see why the two blocks below do not produce the same output.  One displays the numerically sorted array and the other does not. >>> >>> # Block 1 >>> myarray = ["210", "22"] >>> myarray = myarray.sort do |a,b| >>>    a.to_i <=> b.to_i >>> end >>> puts myarray >>> >>> # Block 2 >>> myarray = ["210", "22"] >>> puts myarray.sort do |a,b| >>>    a.to_i <=> b.to_i >>> end >>> >>> In one case, I reassign myarray to the return value of the sort method for myarray and then print myarray.  In the other case, I just directly print the return value of the sort method.  Shouldn't the return value be the same in both cases, and therefore the output also the same? >> >> Precedence. >> >> Note that "puts myarray" returns myarray. > > This is not correct. puts returns nil: I'm so sorry. I knew something was wrong with my explanation but posted it anyway, which was silly. Of course puts returns nil, it says so every time I type it into irb :) -- Paul Smith http://www.nomadicfun.co.uk paul@pollyandpaul.co.uk