Why can't I use attr_accessor inside initializ

2019-04-04 08:41发布

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

3条回答
Emotional °昔
2楼-- · 2019-04-04 09:05

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楼-- · 2019-04-04 09:09

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

查看更多
三岁会撩人
4楼-- · 2019-04-04 09:20

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
查看更多
登录 后发表回答