Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby Ruby Modules Store Inventory Using Modules Extend and Include

Any idea why this isnt working?

module Inventoryable


    module ClassMethods

    def create(attributes)
    object = new(attributes)
    @instances.push(object)
    return object
    end

    def instances
    @instances ||= []
    end


    end #ClassMethods



    def stock_count
    @stockcount ||= 0
    end

    def stock_count=(number)
    @stockcount = number
    end

    def in_stock?
    stock_count > 0
    end
end #Inventoryable


class Shirt
    extend Inventoryable::ClassMethods
    include Inventoryable

    attr_accessor :attributes

    def initialize(attributes)
    @attributes = attributes
    end

end #Shirt


class Trousers

    attr_accessor :attributes

    def initialize(attributes)
    @attributes = attributes
    end

end # Trousers

class Accessory

    attr_accessor :attributes

    def initialize(attributes)
    @attributes = attributes
    end

end #Accessory

shirt1 = Shirt.create(name: "mtf", size: "L")

shirt1.stock_count=(10)

puts "Shirt 1 stock count: %s" % shirt1.stock_count

puts "Shirt 1 is currently in stock: %s " % shirt1.in_stock?

3 Answers

Although you've probably figured it out by now, I thought I'd post this in case others are have the same problem.
On line 8, should not be an instance variable. Instead, line 8 should be

instances.push(object)

That will resolve your error.

Angela Visnesky
Angela Visnesky
20,927 Points

Hi Simon. You are missing

def self.included(klass)
    klass.extend(ClassMethods)
  end

which goes before your module ClassMethods.

That was' nt in this video but i did try it and im still getting the same error (undefined method push for nil class), Think i need to start this bit of the course again .