Remove item from session


#1

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


#2

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


#3

On 6/5/06, Julian ‘Julik’ Tarkhanov removed_email_address@domain.invalid wrote:

the item :myItem is removed from the session.
Thanks in advance.
Rails mailing list
removed_email_address@domain.invalid
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”.


#4

On 6/5/06, Craig W. removed_email_address@domain.invalid wrote:

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

Craig


Rails mailing list
removed_email_address@domain.invalid
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.


#5

On 6/4/06, mic removed_email_address@domain.invalid 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.


#6

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


#7

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