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

**URL:** <https://www.ruby-forum.com/t/fwd-rails-core-strange-behavior-with-session/70841>\
**Category:** Rails\
**Created:** [October 3, 2006, 12:34pm UTC](https://www.ruby-forum.com/t/fwd-rails-core-strange-behavior-with-session/70841 "2006-10-03T12:34:59Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![marek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b2d939/32.png) [@marek](https://www.ruby-forum.com/u/marek)\
**Post date:** [October 3, 2006, 12:34pm UTC](https://www.ruby-forum.com/t/fwd-rails-core-strange-behavior-with-session/70841/1 "2006-10-03T12:34:59Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![marek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b2d939/32.png) [@marek](https://www.ruby-forum.com/u/marek)\
**Post date:** [October 3, 2006, 7:18pm UTC](https://www.ruby-forum.com/t/fwd-rails-core-strange-behavior-with-session/70841/2 "2006-10-03T19:18:24Z")

</div>

On 10/3/06, Marek K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![marek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b2d939/32.png) [@marek](https://www.ruby-forum.com/u/marek)\
**Post date:** [October 3, 2006, 7:34pm UTC](https://www.ruby-forum.com/t/fwd-rails-core-strange-behavior-with-session/70841/3 "2006-10-03T19:34:27Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![marek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b2d939/32.png) [@marek](https://www.ruby-forum.com/u/marek)\
**Post date:** [October 3, 2006, 7:58pm UTC](https://www.ruby-forum.com/t/fwd-rails-core-strange-behavior-with-session/70841/4 "2006-10-03T19:58:28Z")

</div>

On 10/3/06, Marek K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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
