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

kabir k
PLUS
kabir k
Courses Plus Student 18,036 Points

The @ symbol in the Inventoryable module

In the Inventoryable module, why are we using the @ symbol in the first two methods and not in the last one?

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

1 Answer

Milo Winningham
seal-mask
.a{fill-rule:evenodd;}techdegree
Milo Winningham
Web Development Techdegree Student 3,317 Points

The first two methods access the @stock_count instance variable directly. The last method uses the stock_count method, which initializes @stock_count to 0 if it doesn't have a value. If the in_stock? method accessed @stock_count directly, the comparison would fail if the stock count had never been set.

Had the exact same question. Thanks for answering Milo.