Report abuse

module MetaSkills

  def attribute_accessor(name)
    attribute_reader(name)
    attribute_writer(name)
  end

  def attribute_reader(name)
    define_method(name) do
      instance_variable_get "@#{name}"
    end
  end

  def attribute_writer(name)
    define_method "#{name}=" do |value|
      instance_variable_set "@#{name}", value
    end
  end

end

Class.instance_eval do
  include MetaSkills
end

class MyClass
  attribute_accessor :foo
end

o = MyClass.new
o.foo           # => nil
o.foo = 'bar'   # => "bar"
o               # => #<MyClass:0x1001722c0 @foo="bar">