Forum: Ruby on Rails How to clear a session variable?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Rick (Guest)
on 2007-03-30 23:02
(Received via mailing list)
I'm sorry if this is really dumb, but I can't find the answer.

How do I clear (or unset) a session variable?  Not the whole session,
just one item.

I tried treating the session as a hash, but
    session.delete(:mykey)
does not work.
Dutch Rapley (Guest)
on 2007-03-30 23:32
(Received via mailing list)
I typically do

session[:the_var] = nil

someone please correct me if there's a better way.
Jason R. (Guest)
on 2007-03-30 23:38
(Received via mailing list)
session[:key] = nil seems to be the accepted way of doing this.

session is not just a hash, and if you look at the source, #delete takes
no
parameters and actually clears the entire session.

A lot of people (including me) run into this, and I'm still not sure why
it
was done this way. What's wrong with making session.clear  (or
session.empty)
empty it out and keeping session.delete(:key) to work like a hash? The
current implementation definitely violates the Principle of Least
Surprise.

Jason
This topic is locked and can not be replied to.