-
-
Save thiagomoretto/451552 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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