Why can't I use attr_accessor inside initialize?
问题 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