Skip to content

Instantly share code, notes, and snippets.

@gstark
Created January 22, 2010 21:46
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 gstark/284163 to your computer and use it in GitHub Desktop.
Save gstark/284163 to your computer and use it in GitHub Desktop.
require 'test/unit'
module GetterSetter
def getter_setter(attribute)
instance_variable = "@#{instance_variable}"
define_method("#{attribute}=") do |value|
instance_variable_set(instance_variable,value)
end
define_method(attribute) do
instance_variable_get(instance_variable)
end
end
end
class Thing
extend GetterSetter
getter_setter :foo
end
class GetterSetterTest < Test::Unit::TestCase
def test_thing_should_have_foo_setter
a = Thing.new
assert_nothing_raised do
a.foo = 42
end
end
def test_thing_should_have_a_foo_getter
a = Thing.new
a.foo = 42
assert a.foo == 42
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment