Unexpected Hash#first return under Ruby1.9

  • Thread starter Iñaki Baz Castillo
  • Start date
I

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:

irb> h =3D {"aaa"=3D>"AAA", "bbb"=3D>"BBB"}
irb> h.first
["aaa", "AAA"]


I want a method that returns the first value of a hash, rather than an arra=
y=20
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.


=2D-=20
I=C3=B1aki Baz Castillo <[email protected]>
 
J

Jeff Peng

在 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:

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"
 
I

Iñaki Baz Castillo

El Jueves, 21 de Enero de 2010, Jeff Peng escribi=C3=B3:
=E5=9C=A8 2010-01-21=E5=9B=9B=E7=9A=84 22:22 +0900=EF=BC=8CI=C3=B1aki Baz= Castillo=E5=86=99=E9=81=93=EF=BC=9A
=20
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 =3D {"aaa"=3D>"AAA", "bbb"=3D>"BBB"}
irb> h.first
["aaa", "AAA"]
=20
Since the result is an array you can access its element with the array
way:
=20
irb(main):007:0> h.first[1]
=3D> "AAA"

Sure. I just expected Hash#first returning the first valule rather than the=
=20
first [key,value] entry.



=2D-=20
I=C3=B1aki Baz Castillo <[email protected]>
 
B

botp

Sure. I just expected Hash#first returning the first valule rather than t= he
first [key,value] entry.

hashes are pairs(assoc); so hash#first, really means the first pair..

kind regards -botp
 
A

Andrea C. Granata

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:

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"
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
 
I

Iñaki Baz Castillo

El Jueves, 21 de Enero de 2010, Andrea C. Granata escribi=C3=B3:
irb(main):007:0> h.first[1]
=3D> "AAA"

Sure. I just expected Hash#first returning the first valule rather than
the first [key,value] entry.
=20
Hash#first returns the first element. You can access first value with
=20
h.values.first

Thanks, this is "cooler" than doing h.first[1].

:)


=2D-=20
I=C3=B1aki Baz Castillo <[email protected]>
 
J

Jeff Peng

在 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?
 
R

Robert Klemme

2010/1/21 I=F1aki Baz Castillo said:
El Jueves, 21 de Enero de 2010, Andrea C. Granata escribi=F3:
irb(main):007:0> =A0h.first[1]
=3D> =A0"AAA"

Sure. I just expected Hash#first returning the first valule rather tha= n
the first [key,value] entry.

Hash#first returns the first element. =A0You can access first value with

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 =3D h.first
=3D> ["aaa", "AAA"]
irb(main):005:0> v
=3D> "AAA"

or

irb(main):006:0> h.each {|k,v| break v}
=3D> "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}
=3D> {}

:)

Frankly, I'd use h.first.last or h.first[-1] or h.first[1].

Cheers

robert

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

Tony Arcieri

[Note: parts of this message were removed to make it a legal post.]

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top