Question about some code in the agile book


#1

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!


#2

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)
(See what readers are saying! 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)


#3

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.


#4

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


#5

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)
(See what readers are saying! 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)


#6

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 :slight_smile:

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 :slight_smile:

David


Q. What is THE Ruby book for Rails developers?
A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black)
(See what readers are saying! 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)