Hi,

On Fri, 26 May 2006 15:54:21 +0200, <transfire / gmail.com> wrote:

> I'm getting some very strange behavior from a conditional statement --
> I can;t imagine how it is happeneing and I wondering if I stumbled upon
> some sort of bug in Ruby. Although I have no idea how it could even
> occuer, perhaps someone else can see it. Here's the code:

No, that is no bug, it' a feature ;-)

here pkg is a method call
>   p pkg
>   p pkg.gem
>   p pkg.gem
>   if pkg.gem
>       p "HERE"
after this line pkg is a local variable (because you assign to it)
>       pkg = pkg.__merge__( pkg.gem )
>   end
unintialized local variables default to nil, so it prints nil
>   p pkg
>
> This prints
>
>   #<OpenObject....>
>   nil
>   nil
>   nil

If you want the method call you can use pkg().

Here is another example:

$ cat lvar_tst.rb
def a
         "method"
end

def test
         p a
         p a()
         if false
                 a = 23
         end
         p a
         p a()
end

test
$ ruby lvar_tst.rb
"method"
"method"
nil
"method"


This is just the way Ruby (it's parser) handles local variables, but I  
think matz said that this will be made more "consistent" in future  
versions (i.e. if something is a lvar at some point in a scope it will be  
an lvar everywhere in this scope and you might get a warning for your  
above code, like "use of lvar before initialization")

Hope that helps,
Dominik