Ville Mattila wrote:
#  class Foo  < Module
#    def add_attr(name=nil,value=nil)
#      attr_accessor eval(":" + name)
#      eval("@" + name + "=" + value)
#    end
#  end
#  
#  f = Foo.new
#  f.add_attr("bar", "baz")
#  f.bar #-> "baz"
#  f.bar = "biz"
#  f.bar #-> "biz"
#  

Following Guy Decoux's suggestion, how about this?

module DynamicAccessors
  def add_attr(name=nil,value=nil)
    class << self; attr_accessor eval(":" + name); end
    eval("@#{name} = ObjectSpace._id2ref(value.id)")
  end
end

class Foo
include DynamicAccessors
end

f = Foo.new
f.add_attr("bar", "baz")
puts(f.bar) #-> "baz"
f.bar = "biz"
puts(f.bar) #-> "biz"


Regards,

Dennis Decker Jensen