Skip to content

Instantly share code, notes, and snippets.

@jm
Created June 19, 2010 15:36
Show Gist options
  • Save jm/444992 to your computer and use it in GitHub Desktop.
Save jm/444992 to your computer and use it in GitHub Desktop.
class Bar
attr_reader :hash
def method_missing(m, *args)
if m.to_s =~ /((set|get)_(.+))/
case $2
when 'set'
self.class.send(:define_method,$1, lambda {|value| @hash ||= {}; @hash[$3] = value})
send($1, args[0])
when 'get'
self.class.send(:define_method,$1, lambda {@hash ||= {}; @hash[$3]})
send($1)
end
else
super
end
end
end
b = Bar.new
b.set_whateva "dude!!"
puts b.get_whateva
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment