Forum: Ruby on Rails Remove item from session

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.
mic (Guest)
on 2006-06-04 19:20
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
Julian 'Julik' Tarkhanov (Guest)
on 2006-06-05 01:07
(Received via mailing list)
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
Alder G. (Guest)
on 2006-06-05 01:13
(Received via mailing list)
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! ;-)

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".
Craig W. (Guest)
on 2006-06-05 01:19
(Received via mailing list)
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
Alder G. (Guest)
on 2006-06-05 01:41
(Received via mailing list)
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.
Alder G. (Guest)
on 2006-06-05 02:03
(Received via mailing list)
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.
Julian 'Julik' Tarkhanov (Guest)
on 2006-06-05 04:59
(Received via mailing list)
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 :-)

--
Julian 'Julik' Tarkhanov
please send all personal mail to
me at julik.nl
This topic is locked and can not be replied to.