Why can't I use attr_accessor inside initialize?

∥☆過路亽.° 提交于 2019-12-05 12:59:55

问题


I'm trying to do an instance_eval followed by a attr_accessor inside initialize, and I keep getting this: ``initialize': undefined method 'attr_accessor'`. Why isn't this working?

The code looks kind of like this:

class MyClass
   def initialize(*args)
      instance_eval "attr_accessor :#{sym}"
   end
end

回答1:


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



回答2:


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



回答3:


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



来源:https://stackoverflow.com/questions/1734984/why-cant-i-use-attr-accessor-inside-initialize

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