RAA - inverthash

inverthash / 1.0

Short description: Correct Implementation of Hash.invert
Category: Library/Hash
Status: stable
Created: 2004-12-06 22:18:49 GMT
Last update: 2004-12-06 22:18:49 GMT
Owner: Tilo Sloboda (Projects of this owner)
Homepage: http://www.unixgods.org/~tilo/Ruby/invert_hash.html
Download: http://www.unixgods.org/~tilo/Ruby/invert_hash.rb
License: Artistic
Dependency:
None
Description:
Ruby's built-in Hash.invert function is BROKEN!

For a correctly implemented Hash.invert, you would expect:

     h.invert.invert == h 

to be true.. 

But what about:

    h = {"eins"=>1, "uno"=>1, "one"=>1, "two"=>2}

    h.invert
    => {1=>"one", 2=>"two"}

    h.invert.invert
    => {"two"=>2, "one"=>1}

THAT's BROKEN!


what we would expect is this:

    h.invert
    => {1=>["one", "eins", "uno"], 2=>"two"}
    h.invert.invert
    => {"uno"=>1, "two"=>2, "eins"=>1, "one"=>1}
    h.invert.invert == h
    => true

and that's what this implementation does for you..

Edit this project (for project owner)

back to RAA top