What does this method form the 2. agile book do?

def add_product(product)
current_item = @items.find {|item| item.product == product}
if current_item
current_item.increment_quantity
else
@items << CartItem.new(product)
end

Im constantly getting errors when adding this into my code. Does anyonw
know whats wrong with it?

Also, can soemone please explain what the second line in the code does:
current_item = @items.find {|item| item.product == product}

Thanks in advance!

Hi Kristen, the second line searches for a product within the within
the collection of current products. That is

@items.find { |item| item.product == product }

or you can write it as follows

for each item in @items

if item.product == product
current_item = item
break
end

end

Lastly, it seems that you missing an ‘end’ of the last line of the
method.

Good luck,

-Conrad

Hi, the for loop should read…

for item in @items # no each within a ruby for loop

if item.product == product
current_item = item
break
end

end

-Conrad

Lastly, it seems that you missing an ‘end’ of the last line of the
method.

Thanks. That did the trick. I also replaced the second line with the
much simpler for loop. Thanks again guys!