different output for same expression?

S

Steven Arnold

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 =3D ["210", "22"]
myarray =3D myarray.sort do |a,b|
a.to_i <=3D> b.to_i
end
puts myarray=20

# Block 2
myarray =3D ["210", "22"]
puts myarray.sort do |a,b|
a.to_i <=3D> 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 =3D ["210", "22"]
=3D> ["210", "22"]
irb(main):002:0> myarray =3D myarray.sort do |a,b|
irb(main):003:1* a.to_i <=3D> b.to_i
irb(main):004:1> end
=3D> ["22", "210"]
irb(main):005:0> puts myarray=20
22
210
=3D> nil
irb(main):006:0> myarray =3D ["210", "22"]
=3D> ["210", "22"]
irb(main):007:0> puts myarray.sort do |a,b|
irb(main):008:1* a.to_i <=3D> b.to_i
irb(main):009:1> end
210
22
=3D> nil


steven=
 
P

Paul Smith

Forgive me if this is an elementary question and I am missing something o=
bvious, 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. =A0O=
ne displays the numerically sorted array and the other does not.
# Block 1
myarray =3D ["210", "22"]
myarray =3D myarray.sort do |a,b|
=A0 =A0a.to_i <=3D> b.to_i
end
puts myarray

# Block 2
myarray =3D ["210", "22"]
puts myarray.sort do |a,b|
=A0 =A0a.to_i <=3D> b.to_i
end

In one case, I reassign myarray to the return value of the sort method fo=
r myarray and then print myarray. =A0In the other case, I just directly pri=
nt the return value of the sort method. =A0Shouldn't the return value be th=
e 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.
Here is the same thing in the irb interface:


irb(main):001:0> myarray =3D ["210", "22"]
=3D> ["210", "22"]
irb(main):002:0> myarray =3D myarray.sort do |a,b|
irb(main):003:1* =A0 =A0 a.to_i <=3D> b.to_i
irb(main):004:1> end
=3D> ["22", "210"]
irb(main):005:0> puts myarray
22
210
=3D> nil
irb(main):006:0> myarray =3D ["210", "22"]
=3D> ["210", "22"]
irb(main):007:0> puts myarray.sort do |a,b|
irb(main):008:1* =A0 =A0 a.to_i <=3D> b.to_i
irb(main):009:1> end
210
22
=3D> nil


steven



--=20
Paul Smith
http://www.nomadicfun.co.uk

(e-mail address removed)
 
J

Jesús Gabriel y Galán

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. =A0= One displays the numerically sorted array and the other does not.

# Block 1
myarray =3D ["210", "22"]
myarray =3D myarray.sort do |a,b|
=A0 =A0a.to_i <=3D> b.to_i
end
puts myarray

# Block 2
myarray =3D ["210", "22"]
puts myarray.sort do |a,b|
=A0 =A0a.to_i <=3D> b.to_i
end

In one case, I reassign myarray to the return value of the sort method f=
or myarray and then print myarray. =A0In the other case, I just directly pr=
int the return value of the sort method. =A0Shouldn't the return value be t=
he 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:

irb(main):004:0> myarray =3D ["210", "22"]
=3D> ["210", "22"]
irb(main):005:0> puts myarray
210
22
=3D> nil

There's an ambiguity in
your code that's being resolved dur to precedence. =A0Look 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 cont=
ents.
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 <=3D> b.to_i
irb(main):012:2> end)
22
210
=3D> nil

Jesus.
 
P

Paul Smith

2009/12/5 Jes=FAs Gabriel y Gal=E1n said:
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. = =A0One displays the numerically sorted array and the other does not.

# Block 1
myarray =3D ["210", "22"]
myarray =3D myarray.sort do |a,b|
=A0 =A0a.to_i <=3D> b.to_i
end
puts myarray

# Block 2
myarray =3D ["210", "22"]
puts myarray.sort do |a,b|
=A0 =A0a.to_i <=3D> b.to_i
end

In one case, I reassign myarray to the return value of the sort method =
for myarray and then print myarray. =A0In the other case, I just directly p=
rint the return value of the sort method. =A0Shouldn't the return value be =
the same in both cases, and therefore the output also the same?
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 :)

--=20
Paul Smith
http://www.nomadicfun.co.uk

(e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top