From: Dominik Bathon Date: 2006-06-22T07:31:30+09:00 Subject: Re: Using parenthesis/barackets in ruby On Wed, 21 Jun 2006 20:21:28 +0200, Matthew Smillie wrote: > On Jun 21, 2006, at 15:07, Justin Vincent wrote: > >> I was curious to see if ruby could be programmed with >> parenthesis/barackets even though it isn't supposed to. >> >> To my suprise this worked... >> >> Is there any reason why I should not be doing this? > > I think the other posters pretty aptly demonstrated the readability > aspect. On that front, I'll point out that you've got an extra set of > parens around your if/else block: ((if...else...end)). > >> What is it actually doing from a code/processing point of view? > > The parens in your Ruby code aren't acting as block delimiters, they're > just parenthesising expressions. But parens can act as block delimiters in Ruby: >> a = ( ?> p 1 >> p 2 >> 3 >> ) 1 2 => 3 >> a => 3 Or: >> a = (p 1; p 2; 3) 1 2 => 3 Or even: >> p((p 1; p 2; 3)) 1 2 3 => nil Ruby evaluates all expressions inside the parens and the result of the complete expression is the result of the last inner expression. This behaviour can also produce (maybe) surprising results: >> (1 + ?> 2) => 3 but >> (1 >> + 2) => 2 The second expression is evaluated as (1; +2), while the first is evaluated as (1 + 2) (as expected). Dominik