Hello, I''m playing with Agile Web dev for rails, inside which I encountered this model : class Order < ActiveRecord::Base has_many :line_items PAYMENT_TYPES = [ # Displayed stored in db [ "Check" , "check" ], [ "Credit card" , "cc" ], [ "Purchase order" , "po" ] ] # ... validates_presence_of :name, :address, :email, :pay_type validates_inclusion_of :pay_type, :in => PAYMENT_TYPES.map {|disp, value| value} # ... def add_line_items_from_cart(cart) cart.items.each do |item| li = LineItem.from_cart_item(item) line_items << li end end end So far, I know that has_many adds several methods, such as line_items<< and line_items=. I also know that, inside a method, an assignment to some "var" without "self." before it might confuse ruby which can take "var" for a local variable. So using self.var is "safer". But here, there''s no "self.line_items << li". Why does calling "line_items <<" is enough for this piece of code to understand that line_items isn''t a mere variable ? I know that there are some particular cases where "self." isn''t necessary. Maybe are we in one of them ? 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 -~----------~----~----~----~------~----~------~--~---
Arnaud J. wrote:> line_items << li > ... > So far, I know that has_many adds several methods, such as line_items<< > and line_items=. > I also know that, inside a method, an assignment to some "var" without > "self." before it might confuse ruby which can take "var" for a local > variable. So using self.var is "safer". > > But here, there''s no "self.line_items << li". Why does calling > "line_items <<" is enough for this piece of code to understand that > line_items isn''t a mere variable ?"self" is not needed here because the line is not an assignment (no "="). Unlike an assignment, for the line to have any meaning line_items must have been defined previously, so the interpreter sees no ambiguity. -- We develop, watch us RoR, in numbers too big to ignore. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Mark Reginald James wrote:> "self" is not needed here because the line is not an assignment > (no "="). Unlike an assignment, for the line to have any meaning > line_items must have been defined previously, so the interpreter > sees no ambiguity.Got it. I''m not quite used to Ruby yet, that''s why. To me, calling "some_array << 1" was like having an implicit "some_array = []". But it doesn''t work like that. Thank you very much. -- 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 -~----------~----~----~----~------~----~------~--~---