Although a session variable behaves like a hash for purposes of setting
and retrieving values, the documentation tells us that the similarity
ends there. It is not a real hash and therefore does not support
methods like, “has_key?”. What is the recommended methodology for
determining whether a session variable has a particular key? Thanks for
any input.
Although a session variable behaves like a hash for purposes of setting
and retrieving values, the documentation tells us that the similarity
ends there. It is not a real hash and therefore does not support
methods like, “has_key?”. What is the recommended methodology for
determining whether a session variable has a particular key? Thanks for
any input.
This is only a guess, but are you asking a question about Rails?
This is the mailing list for Ruby, the programming language. Rails is an
application/framework which just happens to be written in Ruby. It has
its own mailing lists and forums.
Looking at the source code, it’s pretty icky (I’m looking at ruby
1.8.7p357, and I see for example it sets @write_lock = true at one
point, but never uses it subsequently)
Anyway, if you can avoid storing nil values, then I’d just use
if session[:key]
… do something
end
As has been pointed out above, hardly anyone uses CGI these days; with
any non-trivial code the overhead of loading up libraries and
establishing fresh database connections is too high.
Anyway, if you can avoid storing nil values, then I’d just use
if session[:key]
… do something
end
Good suggestion. One just has to be mindful of the nil-value issue.
Thanks.
Rack::Session::XXX is probably what you want.
That may be. I’d have to look into it. Before reading the responses to
my post, I had never heard of Rack.
As has been pointed out above, hardly anyone uses CGI these days; with
any non-trivial code the overhead of loading up libraries and
establishing fresh database connections is too high.
Earlier you asked me whether my question was about Rails. I said, “No.”
However, I got to wondering why it mattered. Doesn’t Rails use
CGI::Session? If it does, isn’t that fairly good evidence that it’s
quite usable when one understands how to use it? I agree with Marc that
we really could use some improved documentation. I have way more
questions than answers. For example, the documentation says, “By
default, this CGI::Session instance will start a new session if none
currently exists, or continue the current session for this client if one
does exist.” How does it determine whether a session currently exists?
I’m having trouble with new sessions being created when (IMHO) an
existing session should be continued. Kind of in that same vein, I’m
not real clear on exactly what session.close() does.
Anyway, thanks to all. The question that I originally asked has been
answered.
I stand corrected. That’s good info to know. I think that ultimately I
should look into Rack. However, since I am already so far down the
CGI::Session path and since I suspect that a change to Rack would
require major surgery to my code, I would certainly like to get this
working for now and do that as a future major change.
I stand corrected. That’s good info to know. I think that ultimately I
should look into Rack. However, since I am already so far down the
CGI::Session path and since I suspect that a change to Rack would
require major surgery to my code, I would certainly like to get this
working for now and do that as a future major change.
Anyway, if you can avoid storing nil values, then I’d just use
if session[:key]
… do something
end
Interestingly, it seems that I am UNABLE to assign a nil value to a
session element. That’s disappointing because it would have been handy
to treat elements with a nil value as non-existent since there is no way
to remove an element from the pseudo hash.
Note: The above is within the context of CGI::Session.
... doug
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.