Deleting a session item

A few days ago, I spent a little while attempting to work out how to delete an item from a session (in Rails). I wanted to remove session[:user_id] when a user logged out.

After nosing around for a bit, it turns out that you can’t actually do this. You can set the value of :user_id to nil, which is probably “good enough” most of the time. It’s still a little bit irritating however.

The reason that you can’t do this is not because of poor design in Rails though. Rails reuses part of the standard Ruby distribution. And look at CGI::Session.delete. It takes no arguments and deletes the entire session. Contrast this with Hash.delete. To my mind, they both implement the same interface.

It would probably have made more sense to have CGI::Session.destroy, but I doubt that’s going to change now after the code has been in the wild for such a long time.