Skip to content

Instantly share code, notes, and snippets.

@andrewtimberlake
Created February 1, 2011 19:40
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 andrewtimberlake/806490 to your computer and use it in GitHub Desktop.
Save andrewtimberlake/806490 to your computer and use it in GitHub Desktop.
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