开发者

Why can't I use attr_accessor inside initialize?

开发者 https://www.devze.com 2022-12-11 05:31 出处:网络
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

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


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号