Skip to content

Instantly share code, notes, and snippets.

@danielwellman
Created June 19, 2010 18:47
Show Gist options
  • Save danielwellman/445155 to your computer and use it in GitHub Desktop.
Save danielwellman/445155 to your computer and use it in GitHub Desktop.
class ClassWithAttr
def self.my_attr_accessor(attr_name)
define_method("#{attr_name.to_s}=") do |arg|
instance_variable_set("@#{attr_name}", arg)
end
define_method("#{attr_name.to_s}") 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