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]?
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
Jason R. 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.
Hi –
On Wed, 13 Dec 2006, Taylor S. wrote:
Jason R. 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 (Ruby for Rails)
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)
On Wed, Dec 13, 2006 at 03:23:19PM +0100, Taylor S. 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
Tricked again by the syntactic sugar! I read all of this in your book
but my application of the concepts is spotty at best
Hi –
On Wed, 13 Dec 2006, Taylor S. 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 (Ruby for Rails)
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)