From: mame@... Date: 2019-09-17T23:41:04+00:00 Subject: [ruby-core:94950] [Ruby master Bug#16169] rescue in a method argument Issue #16169 has been updated by mame (Yusuke Endoh). I agree that Ruby syntax is very subtle. Ruby's parser is a unspeakable monolith of compromise between human intuition and parser limitation. Consider the following code: ``` foo(x, y, z = ary) ``` It may have two possible interpretations: `foo(x, y, (z = ary))` (foo accepts three arguments and the last one is an assignment expression) and `foo((x, y, z = ary))` (foo accepts one argument that is a multiple assignment statement). To prevent the second interpretation, only an expression is allowed as an argument. `Integer(ENV['FOO']) rescue nil` is a statement, so it cannot be written as an argument. By the way, * A statement can be converted to an expression by surrounding parentheses: `(Integer(ENV['FOO']) rescue nil)` is an expression. So you can write `foo((Integer(ENV['FOO']) rescue nil))`. * Ruby allows to omit parentheses of method calls: `foo x` is considered `foo(x)`. So you can write `foo (Integer(ENV['FOO']) rescue nil)`. Note that the space is required to distinguish from a normal method call `foo(x)`. This is just my understanding. The source code of the parser says "what it does" but doesn't say "why it does (or doesn't)", so we need to surmise. My guess could be wrong. ---------------------------------------- Bug #16169: rescue in a method argument https://bugs.ruby-lang.org/issues/16169#change-81566 * Author: zverok (Victor Shepelev) * Status: Open * Priority: Normal * Assignee: * Target version: * ruby -v: * Backport: 2.5: UNKNOWN, 2.6: UNKNOWN ---------------------------------------- At 2.4, #12686 was introduced, allowing code like this: ```ruby foo (Integer(ENV['FOO']) rescue nil) ``` Though, I noticed that in current Ruby correctness of this syntax depends on space after the method name: ```ruby foo (Integer(ENV['FOO']) rescue nil) # => OK foo(Integer(ENV['FOO']) rescue nil) # SyntaxError ((irb):4: syntax error, unexpected modifier_rescue, expecting ')') # foo(Integer(ENV['FOO']) rescue nil) # ^~~~~~ ``` I wonder, whether it is just a bug or a parser limitation (I can't guess which ambiguity the space-less version produces, but I could be missing something)?.. -- https://bugs.ruby-lang.org/ Unsubscribe: