Created
March 23, 2009 08:49
-
-
Save davidlee/83473 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
| module OrderedHash | |
| def []( index ) | |
| begin | |
| super( index ) | |
| rescue TypeError | |
| x = self.detect do |i| | |
| i.first == index | |
| end # if index.class ... | |
| x && x[1] | |
| end | |
| end | |
| def []=( index, value ) | |
| begin | |
| super( index, value ) | |
| rescue TypeError | |
| x = self.detect do |i| | |
| i.first == index | |
| end # if index.class ... | |
| if x | |
| x[1] = value | |
| else | |
| self << [index, value].extend(OrderedHash) | |
| end | |
| end | |
| end | |
| def keys | |
| map(&:first) | |
| end | |
| def values | |
| map(&:last) | |
| end | |
| end # OrderedHash | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment