How to clear a session variable?


#1

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.


#2

I typically do

session[:the_var] = nil

someone please correct me if there’s a better way.


#3

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