Creating a nested session?

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

Hi --

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

Hi --