[Fwd: [Rails-core] Strange behavior with session]

Hi,

when I create the following method inside a controller which has
sessions enabled, the result I
get (“nil”) is not the one I am expecting (CGI::Session).
Has anybody an explanation for this strange behavior?

— code
def strange
if nil
raise “strange”
session = “whatever”
end
render :text => session.inspect
end

Thanks,

Marek

On 10/3/06, Marek K. [email protected] wrote:

  session = "whatever"
end
render :text => session.inspect

end

This is a Ruby quirk. It ‘sees’ the local variable even if it’s never
assigned when the program executes. For example:

a
NameError: undefined local variable or method `a’ for
#Object:0x2aaaaab232e0
from (irb):1

if false; a = 1 end
=> nil

a
=> nil

jeremy

Hi Jeremy,

thanks for your answer.

Am Dienstag, den 03.10.2006, 10:14 -0700 schrieb Jeremy K.:

This is a Ruby quirk. It ‘sees’ the local variable even if it’s never
assigned when the program executes. For example:

I understand. So a local variable is defined regardless of the program
flow. But why is a local variable assigned, when the method
self.session= exists? That made me some serious headache…

Thanks, Marek

On 10/3/06, Marek K. [email protected] wrote:

assigned when the program executes. For example:

I understand. So a local variable is defined regardless of the program
flow. But why is a local variable assigned, when the method
self.session= exists? That made me some serious headache…

Because in this ambiguous situation you may be assigning a local
variable or
you may be calling a writer method. Ruby interprets it as assigning a
local
variable.

jeremy

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs