From: Rein Henrichs Date: 2010-06-15T08:30:05+09:00 Subject: Re: shorthand On 2010-06-14 15:41:09 -0700, Rick DeNatale said: > On Mon, Jun 14, 2010 at 5:48 PM, Roger Pack wrote: >> I read this once: >> >> Operator ||= can be shorthand for code like: >> �x = "(some fallback value)" unless respond_to? :x or x >> >> How would that look like exactly, in shorthand, any guesses? > > > I don't know where you read that, but it has no basis in reality. The > semantics of ||= have nothing to do with whether or not a method > exists. > > The closest translation of > > x ||= y > > (x || x = y) > > 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 > > -- > 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 Rick, Can we combine the contents of the thousands of messages explaining the behavior of = into a blog post and just start referencing it? Oh wait, didn't you do that? ;) We need a FAQ :D -- Rein Henrichs http://puppetlabs.com http://reinh.com