Project

General

Profile

Actions

Feature #729

closed

curly brackets" and "begin end" blocks should behave syntactically and semantically exactly the same

Added by tpo (Tomas Pospisek) over 15 years ago. Updated about 4 years ago.

Status:
Rejected
Assignee:
-
Target version:
-
[ruby-core:19748]

Description

(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:

# 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:

# 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:

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:

if { condition }
end

and

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.

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0