Unexpected Hash#first return under Ruby1.9

Discussion in 'Ruby' started by Iñaki Baz Castillo, Jan 21, 2010.

  1. 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 <>
     
    Iñaki Baz Castillo, Jan 21, 2010
    #1
    1. Advertising

  2. Iñaki Baz Castillo

    Jeff Peng Guest

    在 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"
     
    Jeff Peng, Jan 21, 2010
    #2
    1. Advertising

  3. 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 <>
     
    Iñaki Baz Castillo, Jan 21, 2010
    #3
  4. Iñaki Baz Castillo

    botp Guest

    On Thu, Jan 21, 2010 at 9:46 PM, I=F1aki Baz Castillo <> wrote=
    :
    > 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
     
    botp, Jan 21, 2010
    #4
  5. 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
    --
    ZephirWorks
     
    Andrea C. Granata, Jan 21, 2010
    #5
  6. 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 <>
     
    Iñaki Baz Castillo, Jan 21, 2010
    #6
  7. Iñaki Baz Castillo

    Jeff Peng Guest

    在 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?
     
    Jeff Peng, Jan 21, 2010
    #7
  8. 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).

    >=20
    > 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=3D>value.

    =2D-=20
    I=C3=B1aki Baz Castillo <>
     
    Iñaki Baz Castillo, Jan 21, 2010
    #8
  9. 2010/1/21 I=F1aki Baz Castillo <>:
    > 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/
     
    Robert Klemme, Jan 21, 2010
    #9
  10. On Jan 21, 2010, at 8:10 AM, Jeff Peng wrote:

    > =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
    >> Hi, Ruby 1.9 implements "first" method for Hash (as Hash are ordered =

    now).
    >=20
    > 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=
     
    James Edward Gray II, Jan 21, 2010
    #10
  11. Iñaki Baz Castillo

    Tony Arcieri Guest

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

    On Thu, Jan 21, 2010 at 7:10 AM, Jeff Peng <> 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.

    --
    Tony Arcieri
    Medioh! A Kudelski Brand
     
    Tony Arcieri, Jan 21, 2010
    #11
  12. El Jueves, 21 de Enero de 2010, James Edward Gray II escribi=C3=B3:
    > On Jan 21, 2010, at 8:10 AM, Jeff Peng wrote:
    > > =E5=9C=A8 2010-01-21=E5=9B=9B=E7=9A=84 22:22 +0900=EF=BC=8CI=C3=B1aki B=

    az Castillo=E5=86=99=E9=81=93=EF=BC=9A
    > >
    > >> Hi, Ruby 1.9 implements "first" method for Hash (as Hash are ordered
    > >> now).

    > >
    > > Just a question, hash in ruby-1.9 is ordered?

    >=20
    > Yes, it now retains insertion order.
    >=20
    > > Then how it calls as hash?

    >=20
    > It's a Hash with super powers. ;)


    It could be called Hasrray...


    =2D-=20
    I=C3=B1aki Baz Castillo <>
     
    Iñaki Baz Castillo, Jan 21, 2010
    #12
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Joe Ardent
    Replies:
    1
    Views:
    362
  2. rp
    Replies:
    1
    Views:
    584
    red floyd
    Nov 10, 2011
  3. Iñaki Baz Castillo
    Replies:
    14
    Views:
    266
    Iñaki Baz Castillo
    Apr 5, 2009
  4. Iñaki Baz Castillo
    Replies:
    7
    Views:
    700
    Rick DeNatale
    Dec 3, 2009
  5. Iñaki Baz Castillo

    Error loading "mysqlplus" under Ruby1.9.1

    Iñaki Baz Castillo, Dec 17, 2009, in forum: Ruby
    Replies:
    5
    Views:
    161
    Roger Pack
    Dec 20, 2009
Loading...

Share This Page