Hi, Ruby 1.9 implements “first” method for Hash (as Hash are ordered
now).
However the return value if a bit annoying for me. A real example:
irb> h = {“aaa”=>“AAA”, “bbb”=>“BBB”}
irb> h.first
[“aaa”, “AAA”]
I want a method that returns the first value of a hash, rather than an
array
containing the first hash element and value. Does such method exist?
Unfortunatelly RDoc for Hash under Ruby 1.9 seems not to exist yet:
http://ruby-doc.org/core-1.9/
Thanks.
在 2010-01-21四的 22:22 +0900,Iñaki Baz C.写é“:
Hi, Ruby 1.9 implements “first” method for Hash (as Hash are ordered now).
However the return value if a bit annoying for me. A real example:
irb> h = {“aaa”=>“AAA”, “bbb”=>“BBB”}
irb> h.first
[“aaa”, “AAA”]
Since the result is an array you can access its element with the array
way:
irb(main):007:0> h.first[1]
=> “AAA”
El Jueves, 21 de Enero de 2010, Jeff P. escribió:
way:
irb(main):007:0> h.first[1]
=> “AAA”
Sure. I just expected Hash#first returning the first valule rather than
the
first [key,value] entry.
On Thu, Jan 21, 2010 at 9:46 PM, Iñaki Baz C. [email protected] wrote:
Sure. I just expected Hash#first returning the first valule rather than the
first [key,value] entry.
hashes are pairs(assoc); so hash#first, really means the first pair…
kind regards -botp
Il 21/01/10 14.46, Iñaki Baz C. ha scritto:
[“aaa”, “AAA”]
Hash#first returns the first element. You can access first value with
h.values.first
Bye,
Andrea
El Jueves, 21 de Enero de 2010, Andrea C. Granata escribió:
irb(main):007:0> h.first[1]
=> “AAA”
Sure. I just expected Hash#first returning the first valule rather than
the first [key,value] entry.
Hash#first returns the first element. You can access first value with
h.values.first
Thanks, this is “cooler” than doing h.first[1].

在 2010-01-21四的 22:22 +0900,Iñaki Baz C.写é“:
Hi, Ruby 1.9 implements “first” method for Hash (as Hash are ordered now).
Just a question, hash in ruby-1.9 is ordered?
Then how it calls as hash?
El Jueves, 21 de Enero de 2010, Jeff P. escribió:
在 2010-01-21四的 22:22 +0900,Iñaki Baz C.写é“:
Hi, Ruby 1.9 implements “first” method for Hash (as Hash are ordered
now).
Just a question, hash in ruby-1.9 is ordered?
Yes:
http://www.igvita.com/2009/02/04/ruby-19-internals-ordered-hash/
Then how it calls as hash?
Because each entry is in the form key=>value.
2010/1/21 Iñaki Baz C. [email protected]:
Thanks, this is “cooler” than doing h.first[1].
… but also might be more expensive because of the potentially large
values Array. Hash#first is probably a bit cheaper because the array
is shorter. You can also do
irb(main):004:0> k,v = h.first
=> [“aaa”, “AAA”]
irb(main):005:0> v
=> “AAA”
or
irb(main):006:0> h.each {|k,v| break v}
=> “AAA”
… which only has the slight disadvantage that it will return the
Hash itself if it is empty:
irb(main):007:0> {}.each {|k,v| break v}
=> {}

Frankly, I’d use h.first.last or h.first[-1] or h.first[1].
Cheers
robert
On Thu, Jan 21, 2010 at 7:10 AM, Jeff P. [email protected] wrote:
Just a question, hash in ruby-1.9 is ordered?
Then how it calls as hash?
It’s an order preserving hash. Hashes and preservation of order are not
mutually exclusive.
El Jueves, 21 de Enero de 2010, James Edward G. II escribió:
Then how it calls as hash?
It’s a Hash with super powers. 
It could be called Hasrray…
On Jan 21, 2010, at 8:10 AM, Jeff P. wrote:
在 2010-01-21四的 22:22 +0900,Iñaki Baz C.写é“:
Hi, Ruby 1.9 implements “first” method for Hash (as Hash are ordered now).
Just a question, hash in ruby-1.9 is ordered?
Yes, it now retains insertion order.
Then how it calls as hash?
It’s a Hash with super powers. 
James Edward G. II