Remove item from session

Hi there,

when i do this:

session[:myItem] = ‘foo’
session[:myItem] = nil

the item :myItem is removed from the session.

But this:

session[‘myItem’] = ‘foo’
session[‘myItem’] = nil

doesn’t remove ‘myItem’, it’s empty but still in the session.

Any suggestion ?

Thanks in advance.

mic

On 4-jun-2006, at 17:20, mic wrote:

session[‘myItem’] = ‘foo’
session[‘myItem’] = nil

doesn’t remove ‘myItem’, it’s empty but still in the session.

Any suggestion ?

Thanks in advance.

Feels like a bug.


Julian ‘Julik’ Tarkhanov
please send all personal mail to
me at julik.nl

On 6/5/06, Julian ‘Julik’ Tarkhanov [email protected] wrote:

the item :myItem is removed from the session.
Thanks in advance.
Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails

It’s not a bug - it’s a feature! :wink:

Seriously: the session object is Ruby stdlib CGI::Session instance.
Which is a pseudo hash, to which you can add keys, and reassign their
values, but not permanently remove keys.

It would be nice to be able to do it, if only for debugging dumps of
#session to be cleaner. But beyond that, I can’t see any real
ramifications for just clearing the values with “session[key] = nil”.

On 6/5/06, Craig W. [email protected] wrote:

But this:
Try session[‘myitem’].delete

Craig


Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails

It won’t work; you’re calling method delete on the object stored in
session[‘myitem’]. In this case, you’d be calling ‘foo’.delete which
is String#delete and in any case has nothing to do with the
CGI::Session instance and/or its (pseudo) keys.

On 6/4/06, mic [email protected] wrote:

Hi there,

when i do this:

session[:myItem] = ‘foo’
session[:myItem] = nil

the item :myItem is removed from the session.

Are you sure about that? It shouldn’t be so, and some simple checks
with both debug(session) indicate it is not, exactly the same as for
the string key.

Moreover, note that even on a regular hash, setting key to value nil
would not eliminate the key. What the CGI::Session pseudo-hash
actually lacks is the Hash#delete method. Or rather, it has its own
#delete, which does something else which you probably don’t want
(destroy the session). Bad design decision for a pseudo-hash object,
imho.

On 4-jun-2006, at 23:10, Alder G. wrote:

ramifications for just clearing the values with “session[key] = nil”.
By “bug” I mean the fact that the result of the two operations is not
consistent. As it turns out I was wrong and you just have to use
session.delete(key) just as with any Ruby hash, indifferent access or
not. Case closed :slight_smile:


Julian ‘Julik’ Tarkhanov
please send all personal mail to
me at julik.nl

On Sun, 2006-06-04 at 17:20 +0200, mic wrote:

session[‘myItem’] = ‘foo’
session[‘myItem’] = nil

doesn’t remove ‘myItem’, it’s empty but still in the session.

Any suggestion ?

Thanks in advance.


Try session[‘myitem’].delete

Craig