Skip to content

Instantly share code, notes, and snippets.

@justinko
Created January 10, 2011 19:42
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 justinko/773325 to your computer and use it in GitHub Desktop.
Save justinko/773325 to your computer and use it in GitHub Desktop.
Subclassing a Ruby core class
class Storage
attr_reader :elements
def initialize
@elements = {}.tap do |elements|
elements[:people] = []
elements[:places] = []
end
end
def size
@elements.size
end
def get(key)
@elements.fetch(key)
end
end
storage = Storage.new
puts storage.inspect
puts storage.size
puts storage.get(:people).inspect
class HashStorage < Hash
def initialize
self[:people] = []
self[:places] = []
end
alias :get :fetch
end
storage = HashStorage.new
puts storage.inspect
puts storage.size
puts storage.get(:people).inspect
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment