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 trialkabir k
Courses Plus Student 18,036 PointsThe @ 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
Web Development Techdegree Student 3,317 PointsThe 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.
Jesse vB
16,959 PointsJesse vB
16,959 PointsHad the exact same question. Thanks for answering Milo.