Created
May 18, 2011 22:56
-
-
Save davidjbeveridge/979777 to your computer and use it in GitHub Desktop.
hacking hash access w/ method_missing
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 Person | |
def initialize | |
@hash = Hash.new | |
end | |
def method_missing(name, value=nil, *args) | |
# Assign only valid keys: | |
if [:name, :dob, :height, :weight].include? name | |
@hash[name] | |
elsif value and [:name=, :dob=, :height=, :weight=].include?(name) | |
@hash[name.to_s.chomp('=').to_sym] = value | |
else | |
super | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment