# Ruby class

**URL:** <https://www.ruby-forum.com/t/ruby-class/184135>\
**Category:** Ruby\
**Created:** [February 25, 2010, 4:34pm UTC](https://www.ruby-forum.com/t/ruby-class/184135 "2010-02-25T16:34:20Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![alexamat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/bc8723/32.png) [@alexamat](https://www.ruby-forum.com/u/alexamat)\
**Post date:** [February 25, 2010, 4:34pm UTC](https://www.ruby-forum.com/t/ruby-class/184135/1 "2010-02-25T16:34:20Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![alexamat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/bc8723/32.png) [@alexamat](https://www.ruby-forum.com/u/alexamat)\
**Post date:** [February 25, 2010, 5:06pm UTC](https://www.ruby-forum.com/t/ruby-class/184135/2 "2010-02-25T17:06:27Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![alexamat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/bc8723/32.png) [@alexamat](https://www.ruby-forum.com/u/alexamat)\
**Post date:** [February 26, 2010, 12:47am UTC](https://www.ruby-forum.com/t/ruby-class/184135/3 "2010-02-26T00:47:10Z")

</div>

On Thu, Feb 25, 2010 at 10:09 AM, Nick B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> 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.

---

<div class="post-metadata">

**Author:** ![alexamat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/bc8723/32.png) [@alexamat](https://www.ruby-forum.com/u/alexamat)\
**Post date:** [February 26, 2010, 7:12am UTC](https://www.ruby-forum.com/t/ruby-class/184135/4 "2010-02-26T07:12:05Z")

</div>

Nick B. wrote:

> 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

---

<div class="post-metadata">

**Author:** ![alexamat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/bc8723/32.png) [@alexamat](https://www.ruby-forum.com/u/alexamat)\
**Post date:** [February 26, 2010, 7:21am UTC](https://www.ruby-forum.com/t/ruby-class/184135/5 "2010-02-26T07:21:48Z")

</div>

Nick B. wrote:

> 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”?

---

<div class="post-metadata">

**Author:** ![alexamat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/bc8723/32.png) [@alexamat](https://www.ruby-forum.com/u/alexamat)\
**Post date:** [February 26, 2010, 9:57am UTC](https://www.ruby-forum.com/t/ruby-class/184135/6 "2010-02-26T09:57:27Z")

</div>

Alex A. wrote:

> 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.

---

<div class="post-metadata">

**Author:** ![alexamat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/bc8723/32.png) [@alexamat](https://www.ruby-forum.com/u/alexamat)\
**Post date:** [February 26, 2010, 10:09am UTC](https://www.ruby-forum.com/t/ruby-class/184135/7 "2010-02-26T10:09:21Z")

</div>

Brian C. wrote:

> Alex A. wrote:
> 
> > 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.

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
