-2

所以我希望能够像这样定义一个类:

class MyHouse < Home
  things :bed, :lamp, :chair
end

Home 负责将这些“东西”放入数组中,如下所示:

class Home
  attr_accessor :things

  def things(*things)
    @things = []
    things.each { |thing| @things << thing }
  end
end

问题是我得到:

NoMethodError: undefined method `things' for MyHouse:Class

我知道有办法做到这一点。帮助表示赞赏,

谢谢,

帕春

4

1 回答 1

3

def things应该def self.things

这使它成为类方法而不是实例方法。

于 2013-01-01T20:30:53.413 回答