How to test whether a session variable has a particular key

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.

  ... doug

Doug J. wrote in post #1070696:

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.

Regards,

Brian.

Perhaps we have to add proper documentation to CGI module.

Or, even better, work on a modern-day replacement of CGI for ruby,
including proper documentation for it.

I’d help for the latter.

Marc H. писал 31.07.2012 15:56:

Perhaps we have to add proper documentation to CGI module.

Or, even better, work on a modern-day replacement of CGI for ruby,
including proper documentation for it.

I’d help for the latter.

It’s called Rack. http://rack.github.com/

Doug J. wrote in post #1070702:

are you asking a question about Rails?

No. My question relates to CGI::Session.

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.

Rack::Session::XXX is probably what you want.

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.

     ... doug

On Tue, Jul 31, 2012 at 2:26 PM, Doug J. [email protected]
wrote:

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?

No. It uses Rack (and Rack middleware).

are you asking a question about Rails?

No. My question relates to CGI::Session.

 ... doug

No. It uses Rack (and Rack middleware).

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.

Thanks.

 ... doug

No. It uses Rack (and Rack middleware).

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.

Thanks.

 ... doug

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.

| Privacy Policy | Terms of Service | Remote Ruby Jobs