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! -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org
2007-Mar-04 03:09 UTC
Re: Question about some code in the agile book
Hi -- On Sun, 4 Mar 2007, Kristen wrote:> > 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.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) --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
> 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. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org
2007-Mar-04 13:02 UTC
Re: Question about some code in the agile book
Hi -- On Sun, 4 Mar 2007, Kristen wrote:> >> 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.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) --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 On Mar 4, 7:02 am, dbl...-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org wrote:> Hi -- > > On Sun, 4 Mar 2007, Kristen wrote: > > >> 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. > > 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)--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org
2007-Mar-05 17:17 UTC
Re: Question about some code in the agile book
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 upThanks -- 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) --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---