conditional delete of a session

Rails 2.2.2 Ruby 1.8.6

ActiveRecord Session Store enabled

For testing purposes I wish to ensure that no session exists. To accomplish this I wrote:

  session.delete if session

But this fails if session == nil and I suspect that it does not work as I anticipate if session exists. In the console I see this behaviour:

$ script/console Loading development environment (Rails 2.2.2)

app.session

NoMethodError: You have a nil object when you didn't expect it! The error occurred while evaluating nil.session         from /usr/lib/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action_controller/test_process.rb:429:in `session'         from (irb):1

app.session.delete if app.session

NoMethodError: You have a nil object when you didn't expect it! The error occurred while evaluating nil.session         from /usr/lib/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action_controller/test_process.rb:429:in `session'         from (irb):2

app.get "/"

=> 200

app.session

=> #<CGI::Session:0xb723ba24 @dbman=#<CGI::Session::CookieStore:0xb723b8e4 @original=nil, ...

app.session.delete if app.session

=>

app.session

=> #<CGI::Session:0xb723ba24 @dbman=#<CGI::Session::CookieStore:0xb723b8e4 @original=nil,

Now, I believed that nil == false and that the construct "object.method if object" should work. Evidently I am misinformed or there is something exceptional about session that I do not know.

How is what I desire, to remove the session if one already exists, accomplished?

James Byrne wrote:

Rails 2.2.2 Ruby 1.8.6

ActiveRecord Session Store enabled

For testing purposes I wish to ensure that no session exists. To accomplish this I wrote:

  session.delete if session

But this fails if session == nil and I suspect that it does not work as I anticipate if session exists. In the console I see this behaviour:

$ script/console Loading development environment (Rails 2.2.2)

app.session

NoMethodError: You have a nil object when you didn't expect it! The error occurred while evaluating nil.session         from /usr/lib/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action_controller/test_process.rb:429:in `session'         from (irb):1

app.session.delete if app.session

NoMethodError: You have a nil object when you didn't expect it! The error occurred while evaluating nil.session         from /usr/lib/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action_controller/test_process.rb:429:in `session'         from (irb):2

app.get "/"

=> 200

app.session

=> #<CGI::Session:0xb723ba24 @dbman=#<CGI::Session::CookieStore:0xb723b8e4 @original=nil, ...

app.session.delete if app.session

=>

app.session

=> #<CGI::Session:0xb723ba24 @dbman=#<CGI::Session::CookieStore:0xb723b8e4 @original=nil,

Now, I believed that nil == false and that the construct "object.method if object" should work. Evidently I am misinformed or there is something exceptional about session that I do not know.

How is what I desire, to remove the session if one already exists, accomplished?

Perhaps

    session.delete if defined?(session) && session