Feature #6096
closedParser tweak - curly braces followed by a coma...
Description
It would be sweet if ruby treated curly braces followed by a coma as a hash rather than as a coma:
def foo(bar, baz)
end
=> nil
foo {}, :baz
SyntaxError: (irb):3: syntax error, unexpected ',', expecting $end
foo {}, :baz
^
from /opt/local/bin/irb:12:in `'
I keep forgetting and run into the issue on a regular basis when writing unit tests... aka:
assert_equal {}, do_stuff
Updated by nobu (Nobuyoshi Nakada) over 12 years ago
- Tracker changed from Bug to Feature
Updated by nobu (Nobuyoshi Nakada) over 12 years ago
- Category set to core
Updated by wardrop (Tom Wardrop) over 12 years ago
I agree, that would be nice, especially given that it would work the other way around, e.g. assert_equal do_stuff, {}. However, there are contexts in which a block literal followed by a comma is valid, e.g. var1, var2 = my_method { }, :something
It's just one of the consequences of optional parenthesis combined with the re-use of the curly bracket syntax. In omitting parenthesis, you introduce a lot of ambiguities - this is consistant with operators which also need parenthesis to disambiguate precedence. Ruby allows you to do that (which is nice), but it has consequences such as what you've reported.
So in some ways, your suggestion is akin to suggesting a change is operator precedence. The fix is obviously to wrap your argument in parenthesis, e.g. assert_equal({}, do_stuff). Or otherwise use Hash.new or Hash[], e.g. assert_equal Hash[], do_stuff. Ruby gives you many options.
Updated by mame (Yusuke Endoh) over 12 years ago
- Status changed from Open to Rejected
I agree with Tom.
Considering Tom's code, it is apparently difficult to fix this issue.
--
Yusuke Endoh mame@tsg.ne.jp