Ruby class

A

Alex Alex

Hello all. I have two classes:
class CartItem
attr_reader :product, :quantity

def initialize(product)
@product = product
@quantity = 1
end

def increment_quantity
@quantity += 1
end

def title
@product.title
end

def price
@product.price * @quantity
end
end


class Cart
attr_reader :items

def initialize
@items = []
end

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

I don't understand, how class Cart can see method "increment_quantity"
from class CartItem?
 
N

Nick Brown

Alex: Because Ruby is a dynamically-typed language, it doesn't have to
"see" the method. At runtime, if current_item happens to be an object
with responds to the 'increment_quantity' method, then
current_item.increment_quantity will work fine.

If the object does not respond to that method, it will raise an error.
 
E

Eric Christopherson

Alex: Because Ruby is a dynamically-typed language, it doesn't have to
"see" the method. At runtime, if current_item happens to be an object
with responds to the 'increment_quantity' method, then
current_item.increment_quantity will work fine.

If the object does not respond to that method, it will raise an error.

And methods are public if not specified otherwise.
 
A

Alex Alex

Nick said:
Alex: Because Ruby is a dynamically-typed language, it doesn't have to
"see" the method. At runtime, if current_item happens to be an object
with responds to the 'increment_quantity' method, then
current_item.increment_quantity will work fine.

If the object does not respond to that method, it will raise an error.

Thanks guys
 
A

Alex Alex

Nick said:
Alex: Because Ruby is a dynamically-typed language, it doesn't have to
"see" the method. At runtime, if current_item happens to be an object
with responds to the 'increment_quantity' method, then
current_item.increment_quantity will work fine.

If the object does not respond to that method, it will raise an error.

And another nuby question:

What class have object "current_item"?
 
B

Brian Candler

Alex said:
What class have object "current_item"?

In general you won't know until you run your code, as you've not shown
any code which adds something to @items. For example:

cart = Cart.new
product = Flurble.new
cart.items << product
cart.add_product(product)

In this case, current_item will be of class Flurble.
 
A

Alex Alex

Brian said:
In general you won't know until you run your code, as you've not shown
any code which adds something to @items. For example:

cart = Cart.new
product = Flurble.new
cart.items << product
cart.add_product(product)

In this case, current_item will be of class Flurble.

Yes, you're right:

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
end

in my case current_item w'll be of class "CartItem"
Thank you for reply
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top