Question

So I want to be able to define a class like this:

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

Where Home takes care of putting those "things" in an array, like this:

class Home
  attr_accessor :things

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

The problem with this is I get:

NoMethodError: undefined method `things' for MyHouse:Class

I know there's a way to do this. Help appreciated,

Thanks,

Pachun

Was it helpful?

Solution

def things should be def self.things

That makes it a class method rather than an instance method.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top