# Question about some code in the agile book

**URL:** <https://www.ruby-forum.com/t/question-about-some-code-in-the-agile-book/87588>\
**Category:** Rails\
**Created:** [March 4, 2007, 3:53am UTC](https://www.ruby-forum.com/t/question-about-some-code-in-the-agile-book/87588 "2007-03-04T03:53:20Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kristen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/c4cdca/32.png) [@Kristen](https://www.ruby-forum.com/u/Kristen)\
**Post date:** [March 4, 2007, 3:53am UTC](https://www.ruby-forum.com/t/question-about-some-code-in-the-agile-book/87588/1 "2007-03-04T03:53:20Z")

</div>

Hello,

My question is based on the following code form the 2ed agile book:

File: depot\_g/app/models/cart\_item.rb

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

File: depot\_g/app/models/cart.rb

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

My question is: how can this line:  
current\_item.increment\_quantity  
call a method from a different class? The line is in cart.rb and its  
calling a method from cart\_item.rb.

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![Kristen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/c4cdca/32.png) [@Kristen](https://www.ruby-forum.com/u/Kristen)\
**Post date:** [March 4, 2007, 4:09am UTC](https://www.ruby-forum.com/t/question-about-some-code-in-the-agile-book/87588/2 "2007-03-04T04:09:22Z")

</div>

Hi –

On Sun, 4 Mar 2007, Kristen wrote:

> @product = product  
> end  
> @items \<\< CartItem.new(product)  
> end  
> end
> 
> My question is: how can this line:  
> current\_item.increment\_quantity  
> call a method from a different class? The line is in cart.rb and its  
> calling a method from cart\_item.rb.

current\_item is an instance of class CartItem, and increment\_quantity is  
an instance method of CartItem. It doesn’t matter what file you’re  
in. The only thing that matters is whether or not the object  
(current\_item) understands the message you’re sending it  
(increment\_quantity).

David

–  
Q. What is THE Ruby book for Rails developers?  
A. RUBY FOR RAILS by David A. Black ([http://www.manning.com/black](http://www.manning.com/black))  
(See what readers are saying! [http://www.rubypal.com/r4rrevs.pdf](http://www.rubypal.com/r4rrevs.pdf))  
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?  
A. Ruby Power and Light, LLC ([http://www.rubypal.com](http://www.rubypal.com))

---

<div class="post-metadata">

**Author:** ![Kristen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/c4cdca/32.png) [@Kristen](https://www.ruby-forum.com/u/Kristen)\
**Post date:** [March 4, 2007, 4:39am UTC](https://www.ruby-forum.com/t/question-about-some-code-in-the-agile-book/87588/3 "2007-03-04T04:39:08Z")

</div>

> current\_item is an instance of class CartItem, and increment\_quantity is  
> an instance method of CartItem. It doesn’t matter what file you’re  
> in. The only thing that matters is whether or not the object  
> (current\_item) understands the message you’re sending it  
> (increment\_quantity).

Hmm, isnt current\_item a local variable? It makes sense that in this  
code the current\_item.increment\_quantity is wokring, but my questions  
how it is working. For me its essential to understand how it works so I  
know how to use in the future . I would like to understand what is  
going on in the background.

Thanks.

---

<div class="post-metadata">

**Author:** ![Kristen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/c4cdca/32.png) [@Kristen](https://www.ruby-forum.com/u/Kristen)\
**Post date:** [March 5, 2007, 4:01pm UTC](https://www.ruby-forum.com/t/question-about-some-code-in-the-agile-book/87588/4 "2007-03-05T16:01:48Z")

</div>

Nice explanation David, you’re a helpful old chap. And your book is  
excellent. I was a beta reader and it was/is a tremendous resource to  
a Rails/Ruby developer

Good work mate

Keep it up

Kirk out

---

<div class="post-metadata">

**Author:** ![Kristen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/c4cdca/32.png) [@Kristen](https://www.ruby-forum.com/u/Kristen)\
**Post date:** [March 5, 2007, 6:19pm UTC](https://www.ruby-forum.com/t/question-about-some-code-in-the-agile-book/87588/5 "2007-03-05T18:19:19Z")

</div>

Hi –

On Mon, 5 Mar 2007, kirkr wrote:

> Nice explanation David, you’re a helpful old chap. And your book is  
> excellent. I was a beta reader and it was/is a tremendous resource to  
> a Rails/Ruby developer
> 
> Good work mate
> 
> Keep it up

Thanks – I shall do my best!

David

–  
Q. What is THE Ruby book for Rails developers?  
A. RUBY FOR RAILS by David A. Black ([http://www.manning.com/black](http://www.manning.com/black))  
(See what readers are saying! [http://www.rubypal.com/r4rrevs.pdf](http://www.rubypal.com/r4rrevs.pdf))  
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?  
A. Ruby Power and Light, LLC ([http://www.rubypal.com](http://www.rubypal.com))

---

<div class="post-metadata">

**Author:** ![Kristen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/c4cdca/32.png) [@Kristen](https://www.ruby-forum.com/u/Kristen)\
**Post date:** [March 4, 2007, 2:02pm UTC](https://www.ruby-forum.com/t/question-about-some-code-in-the-agile-book/87588/6 "2007-03-04T14:02:56Z")

</div>

Hi –

On Sun, 4 Mar 2007, Kristen wrote:

> how it is working. For me its essential to understand how it works so I  
> know how to use in the future . I would like to understand what is  
> going on in the background.

It’s not even the background – it’s all up front 🙂

current\_item is a local variable which happens to refer to a CartItem  
object. You want that CartItem object to do something, so you send it  
the message “increment\_quantity”. The message-sending syntax in Ruby  
takes the form:

object.message

where object can be, and usually is, a variable. “Sending a message”  
is how you tell the object you want it to execute a particular method  
(or trigger whatever unknown-method handlers it may have available to  
it).

The “local” in “local variable” describes its scope. Local variables  
inside method definitions are only in scope inside the definition:

x = 1 # x in outer local scope  
def my\_method  
x = 2 # x in method’s local scope  
puts x # prints 2  
end  
puts x # back to first x, so it prints 1

But, even though they are local in scope, they can have anything  
assigned to them:

class C  
def report  
puts “I’m a C instance!”  
end  
end

def my\_method  
x = C.new  
x.report  
end

my\_method # I’m a C instance!

I definitely agree that it’s a good idea to get a good handle on Ruby  
while you’re learning Rails. There’s even a book written exactly for  
people who are trying to do exactly that 🙂

David

–  
Q. What is THE Ruby book for Rails developers?  
A. RUBY FOR RAILS by David A. Black ([http://www.manning.com/black](http://www.manning.com/black))  
(See what readers are saying! [http://www.rubypal.com/r4rrevs.pdf](http://www.rubypal.com/r4rrevs.pdf))  
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?  
A. Ruby Power and Light, LLC ([http://www.rubypal.com](http://www.rubypal.com))
