Skip to content

Instantly share code, notes, and snippets.

@davidjbeveridge
Created May 18, 2011 22:56
Show Gist options
  • Save davidjbeveridge/979777 to your computer and use it in GitHub Desktop.
Save davidjbeveridge/979777 to your computer and use it in GitHub Desktop.
hacking hash access w/ method_missing
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