(unknown)

Why does this seem to sort ascending alphabetical?

puts ({ :shoe_size => 29.5, :name => 'Fred', :age => 10, :size => 'L' }).to_s

sizeLshoe_size29.5nameFredage10

I'll handle this in two parts:
First what you are doing:
Actually all you are doing is
puts ({ :shoe_size => 29.5, :name => 'Fred', :age => 10, :size =>
'L' })

The to_s applied to the return value of puts, which is nil is the
empty string which is discarded.

I guess that you want to get a nice representation of the hash, try
puts( { :shoe_size => 29.5, :name => 'Fred', :age => 10, :size => 'L'
}.inspect )

Please note that there should not be any spaces bewteen "puts" and "("
as this is deprecated.

Now what you were asking:
The order of the string representation of a Hash is defined as the
order of the Hash, which means it is not. It is completely up to the
interpreter how to iterate over a hash, how to convert it to an array
or to a string.
AFAIK these operations could even be inconsistent if that suited an interpreter.

Use this hash to see that it is not necessarily in alphabetic descending order
{:a=>42, :b=>43, :c=>44}, but be aware that it might be if you use a
different interpreter than I do
irb(main):036:0> RUBY_VERSION
=> "1.8.5"

Cheers
Robert

···

On 4/9/07, John Joyce <dangerwillrobinsondanger@gmail.com> wrote:

Why does this seem to sort ascending alphabetical?

puts ({ :shoe_size => 29.5, :name => 'Fred', :age => 10, :size =>
'L' }).to_s

sizeLshoe_size29.5nameFredage10

--
You see things; and you say Why?
But I dream things that never were; and I say Why not?
-- George Bernard Shaw

well, actually, I knew this part about hashes being unordered.
Which, is why I asked.
I was just wondering if there was some kind of implicit sort occurring. I was unable to find anything in the pickaxe book that would indicate any sort occurring here.
I'm on Ruby 1.8.4 locally.
I'm waiting to upgrade until the version that ships with the next OS X.
Far too much hassle.

Anyway, I've been working through all of the new book "Beginning Ruby, from Novice to Professional" and this example came from there. It kind of threw me, because as I added things to the hash, they seemed to stay in some order, not just first in/last out, pop/push standard stuff.
Maybe my irb is alive and has a sense of humor.

···

On Apr 9, 2007, at 7:22 PM, Robert Dober wrote:

On 4/9/07, John Joyce <dangerwillrobinsondanger@gmail.com> wrote:

Why does this seem to sort ascending alphabetical?

puts ({ :shoe_size => 29.5, :name => 'Fred', :age => 10, :size =>
'L' }).to_s

sizeLshoe_size29.5nameFredage10

I'll handle this in two parts:
First what you are doing:
Actually all you are doing is
puts ({ :shoe_size => 29.5, :name => 'Fred', :age => 10, :size =>
'L' })

The to_s applied to the return value of puts, which is nil is the
empty string which is discarded.

I guess that you want to get a nice representation of the hash, try
puts( { :shoe_size => 29.5, :name => 'Fred', :age => 10, :size => 'L'
}.inspect )

Please note that there should not be any spaces bewteen "puts" and "("
as this is deprecated.

Now what you were asking:
The order of the string representation of a Hash is defined as the
order of the Hash, which means it is not. It is completely up to the
interpreter how to iterate over a hash, how to convert it to an array
or to a string.
AFAIK these operations could even be inconsistent if that suited an interpreter.

Use this hash to see that it is not necessarily in alphabetic descending order
{:a=>42, :b=>43, :c=>44}, but be aware that it might be if you use a
different interpreter than I do
irb(main):036:0> RUBY_VERSION
=> "1.8.5"

Cheers
Robert

--
You see things; and you say Why?
But I dream things that never were; and I say Why not?
-- George Bernard Shaw

by right, u shouldnt really care, since it is, after all, a hash (but
of course we all all curious animals... ;))

I would probably say the same thing as Robert - there is no "standard"
here, and it is all up to the interpreter to do as it wishes.

If u really wish to sort, u could use use '.sort', or '.sort_by'

{5=>:a, 7=>:a, 2=>:a}.sort
{:z=>42, :d=>43, :c=>44}.sort_by {|a| a[0].to_s}

although of course, this also has the effect of transforming the hash
into an array....

-jf

···

On 4/9/07, John Joyce <dangerwillrobinsondanger@gmail.com> wrote:

well, actually, I knew this part about hashes being unordered.
Which, is why I asked.
I was just wondering if there was some kind of implicit sort
occurring.

--
"It's so hard to write a graphics driver that open-sourcing it would not help."
    -- Andrew Fear, Software Product Manager, NVIDIA Corporation