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:
h = {"aaa"=>"AAA", "bbb"=>"BBB"}
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.
···
--
Iñaki Baz Castillo <ibc@aliax.net>
Jeff_Peng
(Jeff Peng)
21 January 2010 13:38
2
在 2010-01-21四的 22:22 +0900,Iñaki Baz Castillo写道:
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:
> h = {"aaa"=>"AAA", "bbb"=>"BBB"}
> 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"
Jeff_Peng
(Jeff Peng)
21 January 2010 14:10
3
在 2010-01-21四的 22:22 +0900,Iñaki Baz Castillo写道:
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?
Sure. I just expected Hash#first returning the first valule rather than the
first [key,value] entry.
···
El Jueves, 21 de Enero de 2010, Jeff Peng escribió:
在 2010-01-21四的 22:22 +0900,Iñaki Baz Castillo写道:
> 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:
>
> > h = {"aaa"=>"AAA", "bbb"=>"BBB"}
> > 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"
--
Iñaki Baz Castillo <ibc@aliax.net>
在 2010-01-21四的 22:22 +0900,Iñaki Baz Castillo写道:
> 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.
···
El Jueves, 21 de Enero de 2010, Jeff Peng escribió:
--
Iñaki Baz Castillo <ibc@aliax.net>
JEG2
(JEG2)
21 January 2010 15:07
6
在 2010-01-21四的 22:22 +0900,Iñaki Baz Castillo写道:
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 Gray II
···
On Jan 21, 2010, at 8:10 AM, Jeff Peng wrote:
It's an order preserving hash. Hashes and preservation of order are not
mutually exclusive.
···
On Thu, Jan 21, 2010 at 7:10 AM, Jeff Peng <jeffpeng@netzero.net> wrote:
Just a question, hash in ruby-1.9 is ordered?
Then how it calls as hash?
--
Tony Arcieri
Medioh! A Kudelski Brand
botp1
(botp)
21 January 2010 13:54
8
hashes are pairs(assoc); so hash#first, really means the first pair..
kind regards -botp
···
On Thu, Jan 21, 2010 at 9:46 PM, Iñaki Baz Castillo <ibc@aliax.net> wrote:
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
Bye,
Andrea
···
Il 21/01/10 14.46, Iñaki Baz Castillo ha scritto:
El Jueves, 21 de Enero de 2010, Jeff Peng escribió:
在 2010-01-21四的 22:22 +0900,Iñaki Baz Castillo写道:
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:
> h = {"aaa"=>"AAA", "bbb"=>"BBB"}
> 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"
Sure. I just expected Hash#first returning the first valule rather than the
first [key,value] entry.
--
ZephirWorks
It could be called Hasrray...
···
El Jueves, 21 de Enero de 2010, James Edward Gray II escribió:
On Jan 21, 2010, at 8:10 AM, Jeff Peng wrote:
> 在 2010-01-21四的 22:22 +0900,Iñaki Baz Castillo写道:
>
>> 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.
--
Iñaki Baz Castillo <ibc@aliax.net>
Thanks, this is "cooler" than doing h.first[1].
···
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
--
Iñaki Baz Castillo <ibc@aliax.net>
Robert_K1
(Robert K.)
21 January 2010 14:30
12
... 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
···
2010/1/21 Iñaki Baz Castillo <ibc@aliax.net>:
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].
--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/