On Sunday 19 February 2006 12:08, Mark James wrote:
> I was under the impression that the if modifier and the
> if statememt were equivalent, but for the following code
> calling A.test raises an exception, while calling A.test2
> succeeds. This is with Ruby 1.8.4.
That's because of the way Ruby decides wether a local variable exists or 
not. Basically, a local variable is defined the first time it is assigned 
to, so in the first case
>    def A.test
>      a if a=1
>    end
a=1, which defines the local, is seen *after* the 'a' statement. So, 'a' 
does not exist and boom

In the second case,
>    def A.test2
>      if a=1
>        a
>      end
>    end
a=1 is seen before the 'a' statement, so it works

Regards,
Sylvain