Created
February 1, 2011 19:40
-
-
Save andrewtimberlake/806490 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 ContactDetails | |
def initialize | |
@hash = Hash.new{ |h,k| h[k] = Array.new } | |
end | |
def history(key) | |
@hash[key] | |
end | |
def method_missing(method_name, *args, &block) | |
method_name = method_name.to_s | |
setter = method_name.chomp!('=') | |
if setter | |
@hash[method_name].delete(args.first) | |
@hash[method_name].unshift(args.first) | |
else | |
@hash[method_name].first | |
end | |
end | |
end | |
contact_details = ContactDetails.new | |
contact_details.work_phone = '011 123 4567' | |
contact_details.work_phone = '011 765 4321' | |
contact_details.work_phone #=> 011 765 4321 | |
contact_details.history('work_phone') #=> ['011 765 4321', '011 123 4567'] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment