Ruby: dynamically generate attribute_accessor

本秂侑毒 提交于 2019-11-27 19:47:30

You need to call the (private) class method attr_accessor on the Event class:

    self.class.send(:attr_accessor, name)

I recommend you add the @ on this line:

    instance_variable_set("@#{name}", value)

And don't use them in the hash.

    data = {:datetime => '2011-11-23', :duration => '90', :class => {:price => '£7', :level => 'all'}}

You could do a bit of meta-magic to solve this, using method_missing:

class Event
  def method_missing(method_name, *args, &block)
    if instance_variable_names.include? "@#{method_name}"
      instance_variable_get "@#{method_name}"
    else
      super
    end
  end
end

What this will do is allow access to object instance variables via object.variable syntax, if the object has those variables defined, without resorting to modifying the entire class via attr_accessor.

attr_accessor is a class method and as such needs to be invoked on the class. It is also a private method, so you need to invoke it in a context in which the class object is self.

As an example:

class C
  def foo
    self.class.instance_eval do
      attr_accessor :baz
    end
  end
end

After creating an instance of C and calling foo on that instance, that instance -- and all future instances -- will contain methods baz and baz=.

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