Skip to content

Instantly share code, notes, and snippets.

@danielwellman
Created June 19, 2010 15:25
Show Gist options
  • Save danielwellman/444982 to your computer and use it in GitHub Desktop.
Save danielwellman/444982 to your computer and use it in GitHub Desktop.
module MyAccessor
def method_missing(method_id, *args)
@storage ||= {}
m = method_id.to_s
missing_accessor = m[/get_(.*)/, 1]
return @storage[missing_accessor] if missing_accessor
missing_setter = m[/set_(.*)/, 1]
return @storage[missing_setter] = args[0] if missing_setter
old_method_missing(method_id, args)
super
end
end
if __FILE__ == $PROGRAM_NAME
require "test/unit"
class TestHashMethod < Test::Unit::TestCase
def test_should_get_stuff
h = Object.new
h.extend MyAccessor
h.set_car "Porsche"
assert_equal "Porsche", h.get_car
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment