Skip to content

Instantly share code, notes, and snippets.

@tmountain
Created December 4, 2012 20:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tmountain/4208415 to your computer and use it in GitHub Desktop.
Save tmountain/4208415 to your computer and use it in GitHub Desktop.
class OrderedHash
def initialize
@data = {}
@order = []
end
def set(key, value)
@data[key] = value
@order << key
end
def get(key)
@data[key]
end
def each
@order.each do |key|
yield key
end
end
end
hash = OrderedHash.new
hash.set('foo', 'bar')
hash.set('bar', 'baz')
hash.set('baz', 'quux')
hash.each do |item|
puts "#{item} -> #{hash.get(item)}"
end
# travis@travis-DX4860:/tmp$ ruby ordered_hash.rb
# foo -> bar
# bar -> baz
# baz -> quux
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment