From: dblack@... Date: 2006-12-24T06:19:18+09:00 Subject: Re: Help with a ruby idiom Hi -- On Sun, 24 Dec 2006, M. Edward (Ed) Borasky wrote: > dblack@wobblini.net wrote: >> Hi -- >> >> On Sun, 24 Dec 2006, M. Edward (Ed) Borasky wrote: >> >>> Might I be a curmudgeon on this Christmas eve eve? I say that any idiom >>> that's confusing enough that its semantics isn't *instantly* recognizable >>> to an old FORTRAN programmer like my curmudgeonly self probably doesn't >>> contribute to code readability. :) So I would write this out in "longhand" >>> and not let the parser have an opportunity to confuse me. So my "idiom" >>> would be >>> >>> if (!a) then >>> a = b >>> end >> >> That won't work though: >> >> ruby -e 'if (!a) then a = b end' >> -e:1: undefined local variable or method `a' for main:Object >> (NameError) >> >> because of the forward reference to a. >> >>> Or, since my curmudgeonly self has managed to learn Perl, :) >>> >>> a = b if !a >> >> ||= is used a lot in Perl, too :-) I guess I'm just used to it; my >> eye takes it in pretty readily. >> >> >> David >> > Does this Perl-ism work? > > if (!defined(a)) then > a = b > end Yes, but in Ruby it's defined? rather than defined. David -- Q. What's a good holiday present for the serious Rails developer? A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black) aka The Ruby book for Rails developers! Q. Where can I get Ruby/Rails on-site training, consulting, coaching? A. Ruby Power and Light, LLC (http://www.rubypal.com)