使用attr_accessor动态创建类属性

9
在Ruby中,是否有一种方法可以动态地向类中添加实例变量?例如:
class MyClass

  def initialize
    create_attribute("name")
  end 

  def create_attribute(name)
    attr_accessor name.to_sym
  end

end

o =  MyClass.new
o.name = "Bob"
o.name
3个回答

27

有一种方法(还有其他方法)是使用instance_variable_setinstance_variable_get,如下所示:

class Test
    def create_method( name, &block )
        self.class.send( :define_method, name, &block )
    end

    def create_attr( name )
        create_method( "#{name}=".to_sym ) { |val| 
            instance_variable_set( "@" + name, val)
        }

        create_method( name.to_sym ) { 
            instance_variable_get( "@" + name ) 
        }
    end
end

t = Test.new
t.create_attr( "bob" )
t.bob = "hello"
puts t.bob

1
我喜欢你的解决方案,它很干净,而且显示了getter和setter。谢谢! - dhulihan
1
你会如何将这个功能转化为一个模块,以便你可以将它与需要这个功能的类混合使用? - ericso

1

可能吧,

instance_variable_set(name,value)

这就是你想要的!

例如:

class Mclass
  def show_variables
    puts self.class.instance_variables
  end
end

Mclass.instance_variable_set(:@test,1)
o=Mclass.new
o.show_variables

你知道,类也是对象。


0
在另一条评论中回答@ericso的评论/问题时有点混乱,所以在这里回答 - 这是一个模块,我使用它(基于@ReeseMoore的代码)来做这个并设置实例变量(必要时)。
# adds ability to dynamically create instance vars & attr_accessors
module Attributable
  def create_method(name, &block)
    self.class.send(:define_method, name.to_sym, &block)
  end

  def create_setter(m)
    create_method("#{m}=".to_sym) { |v| instance_variable_set("@#{m}", v) }
  end

  def create_getter(m)
    create_method(m.to_sym) { instance_variable_get("@#{m}") }
  end

  def set_attr(method, value)
    create_setter method
    send "#{method}=".to_sym, value
    create_getter method
  end
end

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接