Forum: Ruby on Rails Question about some code in the agile book

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
C5b20b09d5d24bd554f703ff0595696b?d=identicon&s=25 Kristen (Guest)
on 2007-03-04 03:53
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!
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 unknown (Guest)
on 2007-03-04 04:09
(Received via mailing list)
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)
C5b20b09d5d24bd554f703ff0595696b?d=identicon&s=25 Kristen (Guest)
on 2007-03-04 04:39
> 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.
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 unknown (Guest)
on 2007-03-04 14:02
(Received via mailing list)
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)
    (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)
F90ae3341c0dae4fb1795075485688c3?d=identicon&s=25 kirkr (Guest)
on 2007-03-05 16:01
(Received via mailing list)
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
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 unknown (Guest)
on 2007-03-05 18:19
(Received via mailing list)
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)
This topic is locked and can not be replied to.