I''m a total beginner at Ruby. I''ve more experience with PHP. I''m trying to figure out how to read this method: def empty_cart @cart = find_cart @cart.empty! @items = @cart.items redirect_to(:action => ''index'') end What does @cart.empty! mean? And does this: redirect_to(:action => ''index'') mean simply that the action index should be called? --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
On 22/11/2006, at 10:24 AM, Jake Barnes wrote:> What does > > @cart.empty! > > mean?Simple. It calls the method called ''empty!'' on the @cart object. It''s a convention in ruby that methods that modify an object in place end in ''!'' and methods that return a boolean end in ''?''.> And does this: > > redirect_to(:action => ''index'')No. This sends a 302 redirect to the browser so that it then requests the index action. Cheers, Pete Yandell --~--~---------~--~----~------------~-------~--~----~ 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
2006-Dec-03 01:00 UTC
Re: newbie question: how to read the "!"
Hi -- On Sun, 3 Dec 2006, Pete Yandell wrote:> > > On 22/11/2006, at 10:24 AM, Jake Barnes wrote: > >> What does >> >> @cart.empty! >> >> mean? > > Simple. It calls the method called ''empty!'' on the @cart object. > > It''s a convention in ruby that methods that modify an object in place > end in ''!'' and methods that return a boolean end in ''?''.That''s sort of a convention-within-a-convention. The basic convention is that !-methods are "dangerous", compared to their non-dangerous counterparts. The "danger" can, but doesn''t have to, take the form of modifying the receiver. There are also lots of methods that change their receivers but don''t end in !, such as Array#pop, Hash#delete, etc. David -- David A. Black | dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3] DABlog (DAB''s Weblog) [2] | Co-director, Ruby Central, Inc. [4] [1] http://www.manning.com/black | [3] http://www.rubypowerandlight.com [2] http://dablog.rubypal.com | [4] http://www.rubycentral.org --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
On Nov 22, 2006, at 00:24 , Jake Barnes wrote:> What does > > @cart.empty! > > mean?Basically, it calls the empty! method on @cart object. Without knowing what class @cart is, it''s impossible to say anything about what it actually does. However, judging by the name, I would guess it empties the cart. The exclamation mark is a naming convention that indicates that the object itself is being modified (as opposed to returning a modified copy of the object).> And does this: > > redirect_to(:action => ''index'') > > mean simply that the action index should be called?Pretty much, but not exactly. It adds a redirect header to the response, indicating that the connected client should initiate a new request, this time for the url identified by :action => ''index''. -- Jakob Skjerning - http://mentalized.net --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---