Project

General

Profile

Feature #729

Updated by nobu (Nobuyoshi Nakada) over 2 years ago

=begin 
  (transported over from rubyforge #16111, as I still think this would be worth while...) 

 
 
  Every now and then I run into a "principle of least surprise" violation wrt blocks in Ruby. 

 
 
  The most primitive problem i have is this: why does the following work: 

 ```ruby 
 
 
    # the code below is grouped together because it represents 
 
    # a semantic whole: 
 
    begin 
   
      do_something 
   
      and_then_something_else 
 
    end 
 ``` 

 
 
  but this here bails out with a syntax error: 

 ```ruby 
 
 
    # the following code is grouped together because it represents 
 
    # a semantic whole: 
 
    { 
   
      do_something 
   
      and_then_something_else 
 
    } 
 ``` 

 
 
  In every other language I know that has curly braces as block delimiters, the above is alowed and _natural_. Except Ruby. 

 
 
  The above code is a special case. I am not sure it can be fixed without breaking Ruby syntax as a whole. I _think_ it can. 

 
 
  To me "curly brackets" and "begin end" should act semantically and syntactically exactly the same. 
 
  I.e. all the following forms should IMHO be allowed and identical: 

 ```ruby 
 
 
    if condition 
 
    end 

 
 
    if condition begin 
 
    end 

 
 
    if condition { 
 
    } 
 ``` 

 
 
  The same would apply to all other ruby control structures. 

 
 
  Unfortunately it seems it is not be possible to fix the general case without breaking Ruby since Ruby expects that "condition" above could also in itself be a block since both: 

 ```ruby 
 
 
    if { condition } 
 
    end 
 ``` 

 
 
  and 

 ```ruby 
 
 
    if begin condition end 
 
    end 
 ``` 

 
 
  are allowed and make sense, but contradict my wish above, since allowing the above proposed change would actually make the syntax more ambiguous and make it _harder_ for the parser to help the programmer with syntax errors. 
 
 =end 

Back