2010/1/21 Iki Baz Castillo <ibc / aliax.net>:
> El Jueves, 21 de Enero de 2010, Andrea C. Granata escribi
>
>> >> irb(main):007:0>  ®۱>> >> => AAA"
>> >
>> > Sure. I just expected Hash#first returning the first valule rather than
>> > the first [key,value] entry.
>>
>> Hash#first returns the first element.    
>>
>> h.values.first
>
> 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

-- 
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/