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 Stock Counts

Grace Kelly
Grace Kelly
33,990 Points

Why does the stock_count method require a "="?

Hey guys I'm just wondering why we add a "=" when defining the stock_count method in the following:

module Inventoryable 

  def stock_count
    @stock_count ||= 0 
  end

  def stock_count=(number) #????
    @stock_count = number
  end

  def in_stock?
    stock_count > 0
  end
end

class Shirt
  include Inventoryable
  attr_accessor :attributes

  def initialize(attributes)
    @attributes = attributes
  end
end

class Pant
  attr_accessor :attributes

  def initialize(attributes)
    @attributes = attributes
  end
end

class Accessory
  attr_accessor :attributes

  def initialize(attributes)
    @attributes = attributes
  end
end



shirt1 = Shirt.new(name: "MTF", size: "L")

shirt1.stock_count = 10
puts "Shirt 1 stock count: %s" % shirt1.stock_count
puts "Shirt 1 in stock?: %s" % shirt1.in_stock?

Thanks in advance for any explanation given!!

1 Answer

Mark Kenney
Mark Kenney
10,148 Points

It is what allows you to set the stock_count with: shirt1.stock_count = 10 Its a Virtual Attribute that creates a setter basically. (http://stackoverflow.com/questions/5398919/what-does-the-equal-symbol-do-when-put-after-the-method-name-in-a-method-d)