Yes I''m one of those folks learning ruby by way of Rails ... I''m confounded by the cart ... I understand this def find_cart session[:cart] ||= Cart.new end Then called as @cart = find_cart What I don''t understand is what''s actually returned here ... I keep expecting to see: session[:cart] = cart Somewhere to save the cart to the session but I don''t see that anywhere. Does the find_cart method return a reference to the session variable? Do all methods return references? ... --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Fluffy Hippo wrote:> Yes I''m one of those folks learning ruby by way of Rails ... I''m confounded > by the cart ... > > I understand this > > def find_cart > session[:cart] ||= Cart.new > end > > Then called as > > @cart = find_cart > > What I don''t understand is what''s actually returned here ... > > I keep expecting to see: > > session[:cart] = cart > > Somewhere to save the cart to the session but I don''t see that anywhere.The ||= is what does the assignment of a new empty cart to the session if it doesn''t already have a cart. Written out very verbosely that does: if session[:cart] return session[:cart] else session[:cart] = Cart.new return session[:cart] end The add_to_cart described later on is what adds individual items to the cart. -- Michael Wang --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
But I guess where I''m hung up on is is it returning the *value* of session[:cart], ie, a cart object with the value of the cart, or is it returning a reference to that actual session[:cart]? I''m missing the magic of how the actions taken on the ''cart'' are actually reflected in the session[:cart] persistent between calls. On 1/4/07, Michael Wang <rails-user-JtyympAsP2K7zZZRDBGcUA@public.gmane.org> wrote:> > > Fluffy Hippo wrote: > > Yes I''m one of those folks learning ruby by way of Rails ... I''m > confounded > > by the cart ... > > > > I understand this > > > > def find_cart > > session[:cart] ||= Cart.new > > end > > > > Then called as > > > > @cart = find_cart > > > > What I don''t understand is what''s actually returned here ... > > > > I keep expecting to see: > > > > session[:cart] = cart > > > > Somewhere to save the cart to the session but I don''t see that anywhere. > > The ||= is what does the assignment of a new empty cart to the session > if it doesn''t already have a cart. Written out very verbosely that does: > > if session[:cart] > return session[:cart] > else > session[:cart] = Cart.new > return session[:cart] > end > > The add_to_cart described later on is what adds individual items to the > cart. > > -- > Michael Wang > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Fluffy Hippo wrote:> But I guess where I''m hung up on is is it returning the *value* of > session[:cart], ie, a cart object with the value of the cart, or is it > returning a reference to that actual session[:cart]? > > I''m missing the magic of how the actions taken on the ''cart'' are actually > reflected in the session[:cart] persistent between calls.The book talks briefly about sessions and how they are stored persistently in that section on carts or you can skip to the reference section and read more about them there. session[:cart] returns whatever object is associated with that key, in this case a Cart object, sort of like this: irb(main):015:0> a = { :id => 1, :name => "blah" } => {:name=>"blah", :id=>1} irb(main):016:0> a[:name] => "blah" irb(main):017:0> session = Hash.new => {} irb(main):018:0> session[:cart] = a => {:name=>"blah", :id=>1} irb(main):019:0> session[:cart] => {:name=>"blah", :id=>1} irb(main):020:0> session[:cart].object_id => 23456251850360 irb(main):021:0> a.object_id => 23456251850360 irb(main):022:0> session.object_id => 23456251831800 -- Michael Wang --~--~---------~--~----~------------~-------~--~----~ 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 Jan 4, 2007, at 3:46 PM, Fluffy Hippo wrote:> But I guess where I''m hung up on is is it returning the *value* of > session[:cart], ie, a cart object with the value of the cart, or is > it returning a reference to that actual session[:cart]? > > I''m missing the magic of how the actions taken on the ''cart'' are > actually reflected in the session[:cart] persistent between calls.The session contains a reference to the cart. Because Rails store away everything referenced from the session at the end of a request, any updates to the cart during that request will be saved. Dave --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Below, find_cart will return either the current cart object (session[:cart]) or if there isn''t a current cart then a new one will be created, saved in session[:cart] and then returned. Later in AWDWR you can find empty_cart where session[:cart] is set to nil. I searched the PDF and surprisingly I don''t see any instances where an updated cart is saved to the session, but I''d expect that in add_to_cart and would look like: session[:cart] = @cart Fluffy Hippo wrote:> Yes I''m one of those folks learning ruby by way of Rails ... I''m > confounded by the cart ... > > I understand this > > def find_cart > session[:cart] ||= Cart.new > end > > Then called as > > @cart = find_cart > > What I don''t understand is what''s actually returned here ... > > I keep expecting to see: > > session[:cart] = cart > > Somewhere to save the cart to the session but I don''t see that anywhere. > > Does the find_cart method return a reference to the session > variable? Do all methods return references? ... >No, @cart is an instance of Cart. There does need to be a place to save an updated cart back into the session. David --~--~---------~--~----~------------~-------~--~----~ 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 Jan 4, 2007, at 4:35 PM, David Schmidt wrote:> Below, find_cart will return either the current cart object (session > [:cart]) or if there isn''t a current cart then a new one will be > created, saved in session[:cart] and then returned. Later in AWDWR > you can find empty_cart where session[:cart] is set to nil. I > searched the PDF and surprisingly I don''t see any instances where > an updated cart is saved to the session, but I''d expect that in > add_to_cart and would look like: > > session[:cart] = @cartThe session already has a reference to the cart set when the request comes in--there''s no need to set it again unless you want to use a new cart object (which you don''t...) Dave --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Ahhh! So there is ''magic'' in the [] method of session that keeps references to returned items? Thus making this a rails thing, not a ruby thing ... guess I''ll have to go dig around and find the session manager in the rails tree and see what it does; but it''s a little clearer now than it was. I was thinking it was a language (ruby) concept I wasn''t groking ... On 1/4/07, Dave Thomas <dave-kbbdpT5sCmpWk0Htik3J/w@public.gmane.org> wrote:> > > > On Jan 4, 2007, at 4:35 PM, David Schmidt wrote: > > > Below, find_cart will return either the current cart object (session > > [:cart]) or if there isn''t a current cart then a new one will be > > created, saved in session[:cart] and then returned. Later in AWDWR > > you can find empty_cart where session[:cart] is set to nil. I > > searched the PDF and surprisingly I don''t see any instances where > > an updated cart is saved to the session, but I''d expect that in > > add_to_cart and would look like: > > > > session[:cart] = @cart > > > The session already has a reference to the cart set when the request > comes in--there''s no need to set it again unless you want to use a > new cart object (which you don''t...) > > > Dave > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Hi, this is a builtin Rails session hash using the Ruby language. Please remember that Ruby is a language and Rails is a framework. Please read "Programminh Ruby" on using a Hash. Then reread the section(s) on using session variables. Good luck, -Conrad On 1/4/07, Fluffy Hippo <fluffyhippo-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Ahhh! So there is ''magic'' in the [] method of session that keeps > references to returned items? Thus making this a rails thing, not a ruby > thing ... guess I''ll have to go dig around and find the session manager in > the rails tree and see what it does; but it''s a little clearer now than it > was. I was thinking it was a language (ruby) concept I wasn''t groking ... > > > On 1/4/07, Dave Thomas <dave-kbbdpT5sCmpWk0Htik3J/w@public.gmane.org> wrote: > > > > > > On Jan 4, 2007, at 4:35 PM, David Schmidt wrote: > > > > > Below, find_cart will return either the current cart object (session > > > [:cart]) or if there isn''t a current cart then a new one will be > > > created, saved in session[:cart] and then returned. Later in AWDWR > > > you can find empty_cart where session[:cart] is set to nil. I > > > searched the PDF and surprisingly I don''t see any instances where > > > an updated cart is saved to the session, but I''d expect that in > > > add_to_cart and would look like: > > > > > > session[:cart] = @cart > > > > > > The session already has a reference to the cart set when the request > > comes in--there''s no need to set it again unless you want to use a > > new cart object (which you don''t...) > > > > > > Dave > > > > > > > > > > > > >--~--~---------~--~----~------------~-------~--~----~ 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 Jan 4, 2007, at 5:31 PM, Fluffy Hippo wrote:> Ahhh! So there is ''magic'' in the [] method of session that keeps > references to returned items? Thus making this a rails thing, > not a ruby thing ... guess I''ll have to go dig around and find the > session manager in the rails tree and see what it does; but it''s a > little clearer now than it was. I was thinking it was a language > (ruby) concept I wasn''t groking ...No, nothing magic: hash = {} cart = [ 1, 2, 3 ] hash[:cart] = cart cart[1] = ''rabbit'' puts hash[:cart] #=> [ 1, '' rabbit'', 3 ] --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Yeah, I''ve just been doing some reading ... my problem was much more fundemental. I didn''t get that everything is a reference in ruby, On 1/4/07, Dave Thomas <dave-kbbdpT5sCmpWk0Htik3J/w@public.gmane.org> wrote:> > > On Jan 4, 2007, at 5:31 PM, Fluffy Hippo wrote: > > Ahhh! So there is ''magic'' in the [] method of session that keeps > references to returned items? Thus making this a rails thing, not a ruby > thing ... guess I''ll have to go dig around and find the session manager in > the rails tree and see what it does; but it''s a little clearer now than it > was. I was thinking it was a language (ruby) concept I wasn''t groking ... > > > > > No, nothing magic: > > hash = {} > cart = [ 1, 2, 3 ] > > hash[:cart] = cart > > cart[1] = ''rabbit'' > > puts hash[:cart] #=> [ 1, '' rabbit'', 3 ] > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Thanks to all. I think I ve got my head around this one now. I''m sure there will be more though;) On 1/4/07, Fluffy Hippo <fluffyhippo-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Yeah, I''ve just been doing some reading ... my problem was much more > fundemental. I didn''t get that everything is a reference in ruby, > > On 1/4/07, Dave Thomas <dave-kbbdpT5sCmpWk0Htik3J/w@public.gmane.org> wrote: > > > > > > On Jan 4, 2007, at 5:31 PM, Fluffy Hippo wrote: > > > > Ahhh! So there is ''magic'' in the [] method of session that keeps > > references to returned items? Thus making this a rails thing, not a ruby > > thing ... guess I''ll have to go dig around and find the session manager in > > the rails tree and see what it does; but it''s a little clearer now than it > > was. I was thinking it was a language (ruby) concept I wasn''t groking ... > > > > > > > > > > No, nothing magic: > > > > hash = {} > > cart = [ 1, 2, 3 ] > > > > hash[:cart] = cart > > > > cart[1] = ''rabbit'' > > > > puts hash[:cart] #=> [ 1, '' rabbit'', 3 ] > > > > > > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Dave Thomas wrote:> > On Jan 4, 2007, at 5:31 PM, Fluffy Hippo wrote: > >> Ahhh! So there is ''magic'' in the [] method of session that keeps >> references to returned items? Thus making this a rails thing, not >> a ruby thing ... guess I''ll have to go dig around and find the >> session manager in the rails tree and see what it does; but it''s a >> little clearer now than it was. I was thinking it was a language >> (ruby) concept I wasn''t groking ... > > > No, nothing magic: > > hash = {} > cart = [ 1, 2, 3 ] > > hash[:cart] = cart > > cart[1] = ''rabbit'' > > puts hash[:cart] #=> [ 1, '' rabbit'', 3 ]Also as I mentioned earlier the "magic" behind the session object is explained in detail in Dave''s book which you are already using. There''s no need to read the actual code if you are just trying to get a basic understanding of how it works. -- Michael Wang --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---