Created
March 11, 2009 23:48
-
-
Save keithrbennett/77823 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Bidirectional Hash | |
| class BiHash | |
| def initialize(a_hash) | |
| raise Exception.new if a_hash == nil | |
| @l2r = {} | |
| @r2l = {} | |
| a_hash.each do |k,v| | |
| @l2r[k] = v | |
| @r2l[v] = k | |
| end | |
| end | |
| def get_right(left) | |
| @l2r[left] | |
| end | |
| def get_left(right) | |
| @r2l[right] | |
| end | |
| end | |
| a_hash = { :color => :yellow, :flavor => :vanilla } | |
| bh = BiHash.new a_hash | |
| puts bh.get_right :color | |
| puts bh.get_left :yellow | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment