Why can't I use attr_accessor inside initialize?

左心房为你撑大大i 提交于 2019-12-04 00:15:16

You can't call attr_accessor on the instance, because attr_accessor is not defined as an instance method of MyClass. It's only available on modules and classes. I suspect you want to call attr_accessor on the instance's metaclass, like this:

class MyClass
  def initialize(varname)
    class <<self
      self
    end.class_eval do
      attr_accessor varname
    end
  end
end

o1 = MyClass.new(:foo)
o2 = MyClass.new(:bar)
o1.foo = "foo" # works
o2.bar = "bar" # works
o2.foo = "baz" # does not work
Rob d'Apice

A cleaner implementation (NB: This will add the accessor to ALL instances of the class, not just the single instance, see comments below):

class MyClass
  def initialize(varname)
    self.class.send(:attr_accessor, varname)
  end
end

Rob d'Apice has it almost right. You just need to write:

self.singleton_class.send(:attr_accessor, varname)

or

self.singleton_class.class_eval "attr_accessor :#{varname}"

or my favorite variant

self.singleton_class.class_exec do attr_accessor varname end

assuming the value of varname is a symbol

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!