Created
January 10, 2011 19:42
-
-
Save justinko/773325 to your computer and use it in GitHub Desktop.
Subclassing a Ruby core class
This file contains 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
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