# Different output for same expression?

**URL:** <https://www.ruby-forum.com/t/different-output-for-same-expression/179859>\
**Category:** Ruby\
**Created:** [December 5, 2009, 6:17pm UTC](https://www.ruby-forum.com/t/different-output-for-same-expression/179859 "2009-12-05T18:17:15Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![steven290](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c57346/32.png) [@steven290](https://www.ruby-forum.com/u/steven290)\
**Post date:** [December 5, 2009, 6:17pm UTC](https://www.ruby-forum.com/t/different-output-for-same-expression/179859/1 "2009-12-05T18:17:15Z")

</div>

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?

Here is the same thing in the irb interface:

irb(main):001:0\> myarray = [“210”, “22”]  
=\> [“210”, “22”]  
irb(main):002:0\> myarray = myarray.sort do |a,b|  
irb(main):003:1\* a.to\_i \<=\> b.to\_i  
irb(main):004:1\> end  
=\> [“22”, “210”]  
irb(main):005:0\> puts myarray  
22  
210  
=\> nil  
irb(main):006:0\> myarray = [“210”, “22”]  
=\> [“210”, “22”]  
irb(main):007:0\> puts myarray.sort do |a,b|  
irb(main):008:1\* a.to\_i \<=\> b.to\_i  
irb(main):009:1\> end  
210  
22  
=\> nil

steven

---

<div class="post-metadata">

**Author:** ![steven290](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c57346/32.png) [@steven290](https://www.ruby-forum.com/u/steven290)\
**Post date:** [December 5, 2009, 6:44pm UTC](https://www.ruby-forum.com/t/different-output-for-same-expression/179859/2 "2009-12-05T18:44:10Z")

</div>

On Sat, Dec 5, 2009 at 5:14 PM, Steven Arnold  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> # 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. There’s an ambiguity in  
your code that’s being resolved dur to precedence. Look at the  
following:

(puts myarray).sort do …

puts (myarray.sort do … )

You’re expecting the second, but you’re getting the first.

However, puts myarray.sort { … will work the way you want, because {  
has higher precedence than do.

> irb(main):005:0\> puts myarray  
> =\> nil
> 
> steven

–  
Paul S.  
[http://www.nomadicfun.co.uk](http://www.nomadicfun.co.uk)

removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![steven290](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c57346/32.png) [@steven290](https://www.ruby-forum.com/u/steven290)\
**Post date:** [December 5, 2009, 10:24pm UTC](https://www.ruby-forum.com/t/different-output-for-same-expression/179859/3 "2009-12-05T22:24:50Z")

</div>

On Sat, Dec 5, 2009 at 6:43 PM, Paul S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > end  
> > Precedence.
> 
> Note that “puts myarray” returns myarray.

This is not correct. puts returns nil:

irb(main):004:0\> myarray = [“210”, “22”]  
=\> [“210”, “22”]  
irb(main):005:0\> puts myarray  
210  
22  
=\> nil

> There’s an ambiguity in  
> your code that’s being resolved dur to precedence. Look at the  
> following:
> 
> (puts myarray).sort do …
> 
> puts (myarray.sort do … )
> 
> You’re expecting the second, but you’re getting the first.

This is not correct. What you are getting is:

puts (myarray.sort) do …

and you are outputting the array in alphanumerical order of its string  
contents.  
The block is passed to the puts method, and silently ignored.

> However, puts myarray.sort { … will work the way you want, because {  
> has higher precedence than do.

This works too if you want to keep the do…end:

irb(main):010:0\> puts(myarray.sort do |a,b|  
irb(main):011:2\* a.to\_i \<=\> b.to\_i  
irb(main):012:2\> end)  
22  
210  
=\> nil

Jesus.

---

<div class="post-metadata">

**Author:** ![steven290](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c57346/32.png) [@steven290](https://www.ruby-forum.com/u/steven290)\
**Post date:** [December 5, 2009, 11:13pm UTC](https://www.ruby-forum.com/t/different-output-for-same-expression/179859/4 "2009-12-05T23:13:24Z")

</div>

2009/12/5 Jesús Gabriel y Galán [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> > > a.to\_i \<=\> b.to\_i
> > 
> > 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 S.  
[http://www.nomadicfun.co.uk](http://www.nomadicfun.co.uk)

removed\_email\_address@domain.invalid
