From: Mark Hubbart Date: 2004-08-13T01:15:05+09:00 Subject: Reasons for precedence? (Was: Curly syntax for muliline blocks...) On Aug 12, 2004, at 3:21 AM, Robert Klemme wrote: > > "Radley Smith" schrieb im Newsbeitrag > news:48fe9df04081203071a457d7d@mail.gmail.com... >> I've seen a mix between the curly syntax and the normal syntax for >> multiline blocks. Which do you prefer? > > Curly for single line or cases where precedence matters, do-end for > multiline. > > I remember a discussion about this some months ago. Maybe you can dig > it > up in the archives. This is how I do it... I can't remember ever having read a suggested reason for why the two block constructs have different precedence. The more I think about it, the less sense it makes to me. For example: a = [1,2,3] ==>[1, 2, 3] b = [[1,1],[2,2],[3,3]] ==>[[1, 1], [2, 2], [3, 3]] c = a.zip b.map {|x,y| x+y} ==>[[1, 2], [2, 4], [3, 6]] c = a.zip b.map do |x,y| x + y end TypeError: Array can't be coerced into Fixnum from (irb):8:in `+' from (irb):8 from (irb):8:in `zip' from (irb):8 Using braces, the block goes to b.map. Using do...end, the block is assigned to a.zip. Fairly straightforward precedence rules. Now, if you want your braces block to go to zip rather than map, you can add parenthesis in an appropriate spot: c = a.zip(b.map) {|x,y| x+y} But if you want to change the precedence for the do...end type block, to make it bind to the map method... c = a.zip( b.map do |x,y| x + y end ) well, that's pretty ugly, and pretty confusing. Putting parens around huge blocks of code like that. I realize this particular example is rather contrived, but obviously these things pop up from time to time in one's code. Is there any particular reason that do...end blocks were given different precedence from {...} blocks? cheers, Mark