From: Max Muermann Date: 2006-10-09T08:09:56+09:00 Subject: Re: || explanation in ruby... in pseudolanguage ------=_Part_6426_4691922.1160348993620 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline On 10/9/06, Tuka Opaleye wrote: > > Hi, > > I am trying to get my head around the || symbol used extensively in > ruby. To me it is somewhat foreign still and I am trying a few exercise > to make it click. Can anyone suggest a phrase (pseudolanguage) that > accurately expresses the possible expressions using || ? Well, || is just the standard boolean OR operator. What makes it a lot more useful in Ruby than in other languages, is that only false and nil are considered to be false, and a boolean or operation always returns the first true (that is, not false or nil) value. That's why nil || 1 is not only true, but returns 1. and 2 || 1 returns 2. This is similar behaviour to the COALESCE function used in SQL: COALESCE( a,b) will return b if a is null, and a otherwise. So, in pseudo-language, first_var || second_var || third_var returns the first non-nil value. There's also ||=, which is a very nice idiom for setting default values: my_param ||= "some default value" will assign "some default value" to my_param only if my_param is nil. Hope that helps, Cheers, Max ------=_Part_6426_4691922.1160348993620--