Skip to content

Instantly share code, notes, and snippets.

@jm
Forked from danielwellman/attr.rb
Created June 19, 2010 18:49
Show Gist options
  • Select an option

  • Save jm/445160 to your computer and use it in GitHub Desktop.

Select an option

Save jm/445160 to your computer and use it in GitHub Desktop.

Revisions

  1. jm revised this gist Jun 19, 2010. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion attr.rb
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    class ClassWithAttr
    def self.my_attr_accessor(attr_name)
    define_method("#{attr_name.to_s}=") do |arg|
    define_method("#{attr_name}=") do |arg|
    instance_variable_set("@#{attr_name}", arg)
    end
    define_method(attr_name) do
  2. jm revised this gist Jun 19, 2010. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion attr.rb
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,7 @@ 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
    define_method(attr_name) do
    instance_variable_get("@#{attr_name}")
    end
    end
  3. @danielwellman danielwellman created this gist Jun 19, 2010.
    30 changes: 30 additions & 0 deletions attr.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,30 @@
    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