On Mon, Jun 14, 2010 at 8:35 PM, Josh Cheek <josh.cheek / gmail.com> wrote:
> On Mon, Jun 14, 2010 at 5:41 PM, Rick DeNatale <rick.denatale / gmail.com>wrote:
>
>> This is close, except that it will blow up if x isn't already defined.
>>
>> another alternative might be
>>
>> (defined? x) ? (x || x = y) : y
>>
>> Which avoids the problem when x isn't defined, but isn't exactly what
>> the 'compiled' ruby code does, whether that 'code' is YARV 'byte
>> codes' or an AST in MRI pre 1.9
>>
>>
> This implies to me that if x is not defined, then return y without modifying
> x. But this contradicts the reason I used ||= for (lazy assignment).
>

Yeah it was a typo on my part

(defined? x) ? (x || x = y) : (x = y)

As I said before though, all of these are approximations.  The
defined? guard is just to avoid the undefined variable error.

For

 x ||= y

The compiler defines x when it sees it, normally a local variable only
gets defined when it actually gets a value assigned.

-- 
Rick DeNatale

Blog: http://talklikeaduck.denhaven2.com/
Github: http://github.com/rubyredrick
Twitter: @RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale