On 5/5/07, [email protected] [email protected] wrote:
require ‘enumerator’
sorted_keys = hash.enum_for(:each_key).to_a.sort
Again, there might very well be a better way to do even that, I’m new to
ruby.
sorted_keys = hash.keys.sort
sorted_keys = hash.keys.sort_by{|k| k.to_s }
as pointed out already.
As an aside:
Will symbols be comparable in Ruby2? Am I off to yet another RCR,
nooo!!!
But as another remark ordered Hashes do not necessarily mean “order by
keys” or “order by values” sometimes you want them ordered by
chronological order where
the hash literal assignment
hsh= {:a=>42, :b=>1764} would be equivalent ro
hsh={}
hsh[:a]=42
hsh[:b]=42*42
of course.
Cheers
Robert
Hi –
On 5/5/07, Robert D. [email protected] wrote:
Something like:
as pointed out already.
I can’t seem to find the first two or three messages in this thread
anywhere (including Google groups), so I may have missed something.
Are only talking about strings and symbols as keys? If so I won’t
bother pointing out that to_s won’t work with integers
David
On 5/5/07, David A. Black [email protected] wrote:
sorted_keys = hash.keys.sort
sorted_keys = hash.keys.sort_by{|k| k.to_s }
as pointed out already.
I can’t seem to find the first two or three messages in this thread
anywhere (including Google groups), so I may have missed something.
Are only talking about strings and symbols as keys? If so I won’t
bother pointing out that to_s won’t work with integers
Hi David,
unfortunately Symbols are not comparable (yet)
516/17 > irb
irb(main):001:0> :a < :b
NoMethodError: undefined method `<’ for Symbol
from (irb):1
that is why I talked about a potential RCR.
Cheers
Robert
Hi –
On 5/5/07, Robert D. [email protected] wrote:
Hi David,
unfortunately Symbols are not comparable (yet)
516/17 > irb
irb(main):001:0> :a < :b
NoMethodError: undefined method `<’ for Symbol
from (irb):1
that is why I talked about a potential RCR.
Right – what I mean is, to_s is OK for strings and symbols but not
for integers.
David
On 5/5/07, David A. Black [email protected] wrote:
Right – what I mean is, to_s is OK for strings and symbols but not
for integers.
Right – what I mean is, I am stupid :(. Should have understood your
post David, sorry.
Robert
On 5/5/07, Gary W. [email protected] wrote:
Yes but
2 < 10
while
2.to_s > 10.to_s
Cheers
Robert
On May 5, 2007, at 4:47 PM, Robert D. wrote:
Yes but
2 < 10
while
2.to_s > 10.to_s
Ah, I was missing the context of ‘sorted keys’.
It seems to me that the desire to have sorted keys and
to also have keys that are not comparable would be
an indication that something isn’t quite right, either
the design is wrong or there are some bugs to be
removed.
Gary W.
On 5/5/07, Gary W. [email protected] wrote:
It seems to me that the desire to have sorted keys and
to also have keys that are not comparable would be
an indication that something isn’t quite right, either
the design is wrong or there are some bugs to be
removed.
I agree with you and when it comes to keys being Strings and Symbols I
always try to convert them to symbols anyway (the uniqueness of keys
in a Hash and symbols just seem to fit).
However, not being able to sort arrays of Symbols automatically
bothers me, but this is maybe slightly OT after all.
Cheers
Robert
Hi –
On 5/5/07, Gary W. [email protected] wrote:
It seems to me that the desire to have sorted keys and
to also have keys that are not comparable would be
an indication that something isn’t quite right, either
the design is wrong or there are some bugs to be
removed.
It’s not that integers aren’t comparable; it’s that they’re an example
of something you wouldn’t want to filter through to_s prior to
comparing.
David
On May 5, 2007, at 3:07 PM, David A. Black wrote:
Right – what I mean is, to_s is OK for strings and symbols but not
for integers.
I’m not following you:
irb(main):001:0> 186.to_s
=> “186”
irb(main):002:0>
Gary W.
On May 5, 2007, at 6:25 PM, David A. Black wrote:
of something you wouldn’t want to filter through to_s prior to
comparing.
I wasn’t clear. My ‘not comparable’ comment was about a hash
with heterogeneous keys (integers, symbols, strings) or a hash
with non-comparable keys (symbols).
I agree with you that the ability to convert everything to a
string via #to_s or even to an integer via #object_id doesn’t
necessarily result in a useful ordering.
Gary W.