Can session hashes only be one level deep? For example, session[:person_id] = @person.id session[:person_full_name] = @person.full_name works where this returns an Array error: session[:person][:id] = @person.id session[:person][:full_name] = @person.full_name Error: You have a nil object when you didn''t expect it! You might have expected an instance of Array. The error occured while evaluating nil.[] Is there no way to nest all the person info inside session[:person]? -- 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 -~----------~----~----~----~------~----~------~--~---
You cannot just assume that new objects will be created out of thin air to fit with the syntax you''ve written. The session is nothing more than a Hash. session[:person] = {} session[:person][:id] = @person.id session[:person][:full_name] = @person.full_name or session[:person] = { :id => @person.id, :full_name => @person.full_name } Jason On 12/13/06, Taylor Strait <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > > Can session hashes only be one level deep? For example, > > session[:person_id] = @person.id > session[:person_full_name] = @person.full_name > > works where this returns an Array error: > > session[:person][:id] = @person.id > session[:person][:full_name] = @person.full_name > > Error: > You have a nil object when you didn''t expect it! > You might have expected an instance of Array. > The error occured while evaluating nil.[] > > Is there no way to nest all the person info inside session[:person]? > > -- > 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 -~----------~----~----~----~------~----~------~--~---
On Wed, Dec 13, 2006 at 03:23:19PM +0100, Taylor Strait wrote: } } Can session hashes only be one level deep? For example, } } session[:person_id] = @person.id } session[:person_full_name] = @person.full_name } } works where this returns an Array error: } } session[:person][:id] = @person.id } session[:person][:full_name] = @person.full_name } } Error: } You have a nil object when you didn''t expect it! } You might have expected an instance of Array. } The error occured while evaluating nil.[] } } Is there no way to nest all the person info inside session[:person]? Remember what you is actually going on here. When you say session[:person_id] = @person.id ...you are calling []= on the session object. That works fine, because session supports []=. When you say session[:person][:id] = @person.id ...you are first calling [] on session and getting a nil back, because it hasn''t been set to anything. You are then calling []= on that nil, which gives you the error you see. Try this instead: session[:person] = { :id => @person.id, :full_name => @person.full_name } With that, session[:person] will contain a hash, and session[:person][:id] will behave as expected. --Greg --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Jason Roelofs wrote:> You cannot just assume that new objects will be created out of thin air > to fit with the syntax you''ve written.But Ruby lets me do this so often! Thanks for the advice. -- 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
2006-Dec-13 15:01 UTC
Re: Creating a nested session?
Hi -- On Wed, 13 Dec 2006, Taylor Strait wrote:> > Jason Roelofs wrote: >> You cannot just assume that new objects will be created out of thin air >> to fit with the syntax you''ve written. > > But Ruby lets me do this so often! Thanks for the advice.Not with nested structures, though, unless they''re specially engineered for it. It all goes back to the fact the [] and []= are actually methods: h = {} h["x"]["y"] .... The inner structure can''t "autovivify", because all that you''re saying here is that h["x"], whatever it may be, has a [] method. That doesn''t narrow it down, since any object can have a [] method. So there''s nothing for Ruby to infer about what it should autovivify the object *to*. That''s different from Perl, for example, where the variables are typed so the interpreter can figure out what inner structure you''re trying to create. David -- Q. What''s a good holiday present for the serious Rails developer? A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black) aka The Ruby book for Rails developers! 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 -~----------~----~----~----~------~----~------~--~---
Tricked again by the syntactic sugar! I read all of this in your book but my application of the concepts is spotty at best :( -- 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
2006-Dec-13 17:00 UTC
Re: Creating a nested session?
Hi -- On Wed, 13 Dec 2006, Taylor Strait wrote:> > Tricked again by the syntactic sugar! I read all of this in your book > but my application of the concepts is spotty at best :(That''s why we have discussion forums :-) Fear not; it does all add up over time. With an occasional relapse, as I can attest :-) But nonetheless. David -- Q. What''s a good holiday present for the serious Rails developer? A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black) aka The Ruby book for Rails developers! 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 -~----------~----~----~----~------~----~------~--~---