Skip to content

Instantly share code, notes, and snippets.

@jm
Forked from danielwellman/attr.rb
Created June 19, 2010 18:49
Show Gist options
  • Save jm/445160 to your computer and use it in GitHub Desktop.
Save jm/445160 to your computer and use it in GitHub Desktop.
class ClassWithAttr
def self.my_attr_accessor(attr_name)
define_method("#{attr_name}=") do |arg|
instance_variable_set("@#{attr_name}", arg)
end
define_method(attr_name) do
instance_variable_get("@#{attr_name}")
end
end
my_attr_accessor :name
end
if __FILE__ == $PROGRAM_NAME
require "test/unit"
require 'rubygems'
require 'mocha'
class AttrTest < Test::Unit::TestCase
def test_my_attr_accessor
person = ClassWithAttr.new
person.name = "Dan"
assert_equal "Dan", person.name
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment