Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Ruby

Has the extend method changed

This is the second video i have been following when i get the error cant find variable when trying to extend a module. In the text editor the word extend is not highlighted as i would expect so has this changed since the video? Heres the code and the when i run it it says the instances variable cant find?

module Tracking



    def instances
    @instances ||= []
    end
end
class Customer
extend Tracking
    attr_reader :name

    def initialize(name)
    @name = name
    end

    def to_s
    "[#{name}]"
    end
end

It's tricky to help without knowing exactly how you're calling the 'instances' method, and what exact error message you're getting.

The way 'extend' works hasn't changed, but I'm wondering if you're not calling the 'instances' method on an instance of Customer and not on the Customer class itself.

The 'extend' keyword adds class methods, and not instance methods. Because this code calls 'instances' on an instance of Customer (and not on Customer itself), it will return a NoMethodError and tell you the 'instances' method is undefined:

new_customer = Customer.new("Greg")
new_customer.instances

However, this code will work just fine:

Customer.instances