Skip to content

Instantly share code, notes, and snippets.

@davidlee
Created March 23, 2009 08:49
Show Gist options
  • Select an option

  • Save davidlee/83473 to your computer and use it in GitHub Desktop.

Select an option

Save davidlee/83473 to your computer and use it in GitHub Desktop.
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