Hash Reverse ?

Discussion in 'Ruby' started by salai, Oct 26, 2009.

  1. salai

    salai Guest

    I am preparing for my orals exam in Ruby (my last chance.. , If I fail
    the test, no more possible to study Computer Science related :) )

    http://codepad.org/foNb9Pd3

    class Hash
    def hash_revert
    hash_new = Hash.new
    self.each {|key,value|
    if not hash_new.has_key?(key) then hash_new[value] = key end
    }
    return hash_new
    end
    end

    h = {2=>"a", 1=> "b", 3 =>"a", 4=> "a", 5 => "b", 6=>"c"}
    p h.hash_revert #--> {"a"=>4, "b"=>1, "c"=>6}

    or Should I get this ??
    # => {"a"=>2, "b"=>1, "c"=>6}

    Because HASH is working like "Set" ??

    many thanks in advance,
    salai.
    salai, Oct 26, 2009
    #1
    1. Advertising

  2. Salai Koko wrote:
    > I am preparing for my orals exam in Ruby (my last chance.. , If I fail
    > the test, no more possible to study Computer Science related :) )
    >
    > http://codepad.org/foNb9Pd3
    >
    > class Hash
    > def hash_revert
    > hash_new = Hash.new
    > self.each {|key,value|
    > if not hash_new.has_key?(key) then hash_new[value] = key end
    > }
    > return hash_new
    > end
    > end
    >
    > h = {2=>"a", 1=> "b", 3 =>"a", 4=> "a", 5 => "b", 6=>"c"}
    > p h.hash_revert #--> {"a"=>4, "b"=>1, "c"=>6}
    >
    > or Should I get this ??
    > # => {"a"=>2, "b"=>1, "c"=>6}
    >
    > Because HASH is working like "Set" ??
    >
    > many thanks in advance,
    > salai.


    In Ruby 1.9, hashes keep the order of input. Before Ruby 1.9, the order
    is "random" - so it's not guaranteed which will be the first key that
    you will see with 'each'.
    --
    Posted via http://www.ruby-forum.com/.
    Aldric Giacomoni, Oct 26, 2009
    #2
    1. Advertising

  3. 2009/10/26 Aldric Giacomoni <>:
    > Salai Koko wrote:
    >> I am preparing for my orals exam in Ruby (my last chance.. , If I fail
    >> the test, no more possible to study Computer Science related :) )
    >>
    >> http://codepad.org/foNb9Pd3
    >>
    >> class Hash
    >> =A0 def hash_revert
    >> =A0 =A0 hash_new =3D Hash.new
    >> =A0 =A0 self.each {|key,value|
    >> =A0 =A0 =A0 if not hash_new.has_key?(key) then hash_new[value] =3D key e=

    nd
    >> =A0 =A0 }
    >> =A0 =A0 return hash_new
    >> =A0 end
    >> end
    >>
    >> h =3D {2=3D>"a", 1=3D> "b", 3 =3D>"a", 4=3D> "a", 5 =3D> "b", 6=3D>"c"}
    >> p h.hash_revert #--> {"a"=3D>4, "b"=3D>1, "c"=3D>6}
    >>
    >> or Should I get this ??
    >> # =3D> {"a"=3D>2, "b"=3D>1, "c"=3D>6}
    >>
    >> Because HASH is working like "Set" ??
    >>
    >> many thanks in advance,
    >> salai.

    >
    > In Ruby 1.9, hashes keep the order of input. Before Ruby 1.9, the order
    > is "random" - so it's not guaranteed which will be the first key that
    > you will see with 'each'.


    As far as I can see salai does not want to reverse the order of
    elements but rather the key value relationship.

    Salai, it really depends on the requirements. Here's another valid variant=
    :

    class Hash
    def hash_revert
    r =3D Hash.new {|h,k| h[k] =3D []}
    each {|k,v| r[v] << k}
    r
    end
    end

    irb(main):008:0> {2=3D>"a", 1=3D> "b", 3 =3D>"a", 4=3D> "a", 5 =3D> "b",
    6=3D>"c"}.hash_revert
    =3D> {"a"=3D>[2, 3, 4], "b"=3D>[1, 5], "c"=3D>[6]}

    http://codepad.org/XNvwvOxi

    Kind regards

    robert

    --=20
    remember.guy do |as, often| as.you_can - without end
    http://blog.rubybestpractices.com/
    Robert Klemme, Oct 26, 2009
    #3
  4. On Mon, Oct 26, 2009 at 8:59 AM, salai <> wrote:

    > h = {2=>"a", 1=> "b", 3 =>"a", 4=> "a", 5 => "b", 6=>"c"}
    > p h.hash_revert #--> {"a"=>4, "b"=>1, "c"=>6}
    >
    > or Should I get this ??
    > # => {"a"=>2, "b"=>1, "c"=>6}
    >
    > Because HASH is working like "Set" ??


    See Hash#invert and perhaps its implementation.

    -greg
    Gregory Brown, Oct 26, 2009
    #4
  5. On Mon, Oct 26, 2009 at 8:59 AM, salai <> wrote:
    > I am preparing for my orals exam in Ruby (my last chance.. , If I fail
    > the test, no more possible to study Computer Science related :) )
    >
    > http://codepad.org/foNb9Pd3


    codepad.org idea is wicked, new bookmark for me =)


    --
    Kind Regards,
    Rajinder Yadav

    http://DevMentor.org

    Do Good! - Share Freely, Enrich and Empower people to Transform their lives.
    Rajinder Yadav, Oct 26, 2009
    #5
  6. Rajinder Yadav wrote:
    > On Mon, Oct 26, 2009 at 8:59 AM, salai <> wrote:
    >> I am preparing for my orals exam in Ruby (my last chance.. , If I fail
    >> the test, no more possible to study Computer Science related :) )
    >>
    >> http://codepad.org/foNb9Pd3

    >
    > codepad.org idea is wicked, new bookmark for me =)
    >

    I recommend pastie.org , actually.
    --
    Posted via http://www.ruby-forum.com/.
    Aldric Giacomoni, Oct 26, 2009
    #6
  7. [Note: parts of this message were removed to make it a legal post.]

    Good Morning,

    On Mon, Oct 26, 2009 at 5:59 AM, salai <> wrote:

    > I am preparing for my orals exam in Ruby (my last chance.. , If I fail
    > the test, no more possible to study Computer Science related :) )
    >
    > http://codepad.org/foNb9Pd3
    >
    > class Hash
    > def hash_revert
    > hash_new = Hash.new
    > self.each {|key,value|
    > if not hash_new.has_key?(key) then hash_new[value] =
    > key end
    > }
    > return hash_new
    > end
    > end
    >
    > h = {2=>"a", 1=> "b", 3 =>"a", 4=> "a", 5 => "b", 6=>"c"}
    > p h.hash_revert #--> {"a"=>4, "b"=>1, "c"=>6}
    >
    > or Should I get this ??
    > # => {"a"=>2, "b"=>1, "c"=>6}
    >
    >

    The answer can be different (and most likely will be) under 1.8 and 1.9 - in
    1.8 order is not guaranteed - but in 1.9 it is (order of insertion into the
    hash) and therefore under 1.9 you will get the second option and apparantly
    under 1.8 you get your first.

    John
    John W Higgins, Oct 26, 2009
    #7
  8. On Mon, Oct 26, 2009 at 2:34 PM, Aldric Giacomoni <> wrote:
    > Rajinder Yadav wrote:
    >> On Mon, Oct 26, 2009 at 8:59 AM, salai <> wrote:
    >>> I am preparing for my orals exam in Ruby (my last chance.. , If I fail
    >>> the test, no more possible to study Computer Science related :) )
    >>>
    >>> http://codepad.org/foNb9Pd3

    >>
    >> codepad.org idea is wicked, new bookmark for me =)
    >>

    > I recommend pastie.org , actually.


    But the two are not the same thing. Pastie is a nice pastebin
    service, for sure (I typically use it myself when I don't need the
    features gist has to offer)
    But codepad.org actually executes the code pasted in. That is what is
    neat about it.

    -greg
    Gregory Brown, Oct 26, 2009
    #8
  9. salai

    salai Guest

    Dear All,

    Thank you very much to you all, I passed the exam.

    regards,
    Salai

    On Mon, Oct 26, 2009 at 8:57 PM, Gregory Brown
    <> wrote:
    > On Mon, Oct 26, 2009 at 2:34 PM, Aldric Giacomoni <> wr=

    ote:
    >> Rajinder Yadav wrote:
    >>> On Mon, Oct 26, 2009 at 8:59 AM, salai <> wrote:
    >>>> I am preparing for my orals exam in Ruby (my last chance.. , If I fail
    >>>> the test, no more possible to study Computer Science related :) )
    >>>>
    >>>> http://codepad.org/foNb9Pd3
    >>>
    >>> codepad.org idea is wicked, new bookmark for me =3D)
    >>>

    >> I recommend pastie.org , actually.

    >
    > But the two are not the same thing. =A0Pastie is a nice pastebin
    > service, for sure (I typically use it myself when I don't need the
    > features gist has to offer)
    > But codepad.org actually executes the code pasted in. =A0That is what is
    > neat about it.
    >
    > -greg
    >
    >
    salai, Oct 27, 2009
    #9
    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. Red Orchid
    Replies:
    3
    Views:
    1,038
  2. dogbite
    Replies:
    4
    Views:
    691
    osmium
    Oct 10, 2003
  3. Pieter Claassen
    Replies:
    1
    Views:
    1,106
    CBFalconer
    Aug 4, 2004
  4. rp
    Replies:
    1
    Views:
    512
    red floyd
    Nov 10, 2011
  5. Srijayanth Sridhar
    Replies:
    19
    Views:
    610
    David A. Black
    Jul 2, 2008
Loading...

Share This Page