From: Farhad Farzaneh on
Ryan Davis wrote:
> On Feb 18, 2010, at 13:44 , Farhad Farzaneh wrote:
>
>>>> irb(main):002:0> unless defined?(fooo) ; fooo = true ; end
>>
>> Cool, any chance you could give a short description for those of us that
>> have never really thought about the parser or used parse_tree_show?
>
> Your latter code snippet treats "fooo" in defined? as a method call.
> This is because the assignment inside the conditional hasn't been parsed
> yet, and hasn't affected the lookup tables.
>
> The former doesn't have this problem because the body of the conditional
> is parsed first.

Thanks. To make sure I understand: In the first case,

> foo = true unless defined?(foo)

the parser encounters 'foo', adds it to the symbol table, and then sees
the defined? call, so the net result is that foo is defined, but has nil
value. As such, if we're ever going to use defined? as a conditional,
it should precede any other mention of the token (foo).

Is this correct?
--
Posted via http://www.ruby-forum.com/.

From: Ryan Davis on

On Feb 18, 2010, at 16:04 , Farhad Farzaneh wrote:

> Ryan Davis wrote:
>> On Feb 18, 2010, at 13:44 , Farhad Farzaneh wrote:
>>
>>>>> irb(main):002:0> unless defined?(fooo) ; fooo = true ; end
>>>
>>> Cool, any chance you could give a short description for those of us that
>>> have never really thought about the parser or used parse_tree_show?
>>
>> Your latter code snippet treats "fooo" in defined? as a method call.
>> This is because the assignment inside the conditional hasn't been parsed
>> yet, and hasn't affected the lookup tables.
>>
>> The former doesn't have this problem because the body of the conditional
>> is parsed first.
>
> Thanks. To make sure I understand: In the first case,
>
>> foo = true unless defined?(foo)
>
> the parser encounters 'foo', adds it to the symbol table, and then sees
> the defined? call, so the net result is that foo is defined, but has nil
> value. As such, if we're ever going to use defined? as a conditional,
> it should precede any other mention of the token (foo).

No, it isn't necessarily defined. AT PARSE TIME it sees the initial foo and decides it is a local variable so it treats the foo in defined?(foo) as a local variable. AT RUN TIME it evaluates the defined?(foo) and acts accordingly.


From: Ryan Davis on

On Feb 18, 2010, at 16:04 , Farhad Farzaneh wrote:

> Ryan Davis wrote:
>> On Feb 18, 2010, at 13:44 , Farhad Farzaneh wrote:
>>
>>>>> irb(main):002:0> unless defined?(fooo) ; fooo = true ; end
>>>
>>> Cool, any chance you could give a short description for those of us that
>>> have never really thought about the parser or used parse_tree_show?
>>
>> Your latter code snippet treats "fooo" in defined? as a method call.
>> This is because the assignment inside the conditional hasn't been parsed
>> yet, and hasn't affected the lookup tables.
>>
>> The former doesn't have this problem because the body of the conditional
>> is parsed first.
>
> Thanks. To make sure I understand: In the first case,
>
>> foo = true unless defined?(foo)
>
> the parser encounters 'foo', adds it to the symbol table, and then sees
> the defined? call, so the net result is that foo is defined, but has nil
> value. As such, if we're ever going to use defined? as a conditional,
> it should precede any other mention of the token (foo).

I should also point out: almost all of this is of no consequence. You almost never use defined? on a local variable like this. You usually use it on a const, ivar, cvar, or global, and all of those are unambiguous.



From: Farhad Farzaneh on
Ryan Davis wrote:
> On Feb 18, 2010, at 16:04 , Farhad Farzaneh wrote:
>
>>> yet, and hasn't affected the lookup tables.
>> value. As such, if we're ever going to use defined? as a conditional,
>> it should precede any other mention of the token (foo).
>
> I should also point out: almost all of this is of no consequence. You
> almost never use defined? on a local variable like this. You usually use
> it on a const, ivar, cvar, or global, and all of those are unambiguous.

Thanks. I sometimes use it in Rails partials rendering, where I may
pass an optional local variable, but if it isn't passed, I want to set
it to some default value. Perhaps there's a better way of doing this...
--
Posted via http://www.ruby-forum.com/.

From: Albert Schlef on
Farhad Farzaneh wrote:
>> I should also point out: almost all of this is of no consequence. You
>> almost never use defined? on a local variable like this. You usually use
>> it on a const, ivar, cvar, or global, and all of those are unambiguous.
>
> Thanks. I sometimes use it in Rails partials rendering, where I may
> pass an optional local variable, but if it isn't passed, I want to set
> it to some default value.

BTW, I wonder,

In templates, local variables are created dynamically (e.g., the
programmer passes a hash of "variables" to the template engine). So how
can Ruby know, in the template code, that a "name" refers to a variable?
By default Ruby thinks names are method invocations and since there's no
assignment, Ruby has no way to know these are variables...


--
Posted via http://www.ruby-forum.com/.

First  |  Prev  |  Next  |  Last
Pages: 1 2 3 4
Prev: DXF Writer?
Next: System handling of undef_method