ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
* [ruby-core:122335] [Ruby Bug#21382] Syntax for arguments in || is more strict than arguments in ()
@ 2025-05-29  7:29 herwin (Herwin W) via ruby-core
  2025-06-05  8:40 ` [ruby-core:122434] " matz (Yukihiro Matsumoto) via ruby-core
  0 siblings, 1 reply; 2+ messages in thread
From: herwin (Herwin W) via ruby-core @ 2025-05-29  7:29 UTC (permalink / raw)
  To: ruby-core; +Cc: herwin (Herwin W)

Issue #21382 has been reported by herwin (Herwin W).

----------------------------------------
Bug #21382: Syntax for arguments in || is more strict than arguments in ()
https://bugs.ruby-lang.org/issues/21382

* Author: herwin (Herwin W)
* Status: Open
* ruby -v: ruby 3.4.4 (2025-05-14 revision a38531fd3f) +PRISM [x86_64-linux] (seen in older versions too)
* Backport: 3.2: UNKNOWN, 3.3: UNKNOWN, 3.4: UNKNOWN
----------------------------------------
```ruby
p ->(x, y = x + 1) { x + y }.call(1)
```
This works fine, this gives `y` the value of `x + 1` and prints `3`. The same thing can be used in method definitions.

```ruby
p lambda { |x, y = x + 1| x + y }.call(1)
```
This is the same thing, but results in a parse error (both prism and parse.y):
```
# Prism:
p lambda { |x, y = x + 1| x + y }.call(1)
                    ^ expected the block parameters to end with `|`
                     ^ unexpected '+', ignoring it

# Parse.y:
syntax error, unexpected '+', expecting '|'
```
It works if the default is a single statement (wrapping it in parentheses), so it looks to be purely a grammatical issue
```ruby
p lambda { |x, y = (x + 1)| x + y }.call(1)
```



-- 
https://bugs.ruby-lang.org/
______________________________________________
 ruby-core mailing list -- ruby-core@ml.ruby-lang.org
 To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
 ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/

^ permalink raw reply	[flat|nested] 2+ messages in thread

* [ruby-core:122434] [Ruby Bug#21382] Syntax for arguments in || is more strict than arguments in ()
  2025-05-29  7:29 [ruby-core:122335] [Ruby Bug#21382] Syntax for arguments in || is more strict than arguments in () herwin (Herwin W) via ruby-core
@ 2025-06-05  8:40 ` matz (Yukihiro Matsumoto) via ruby-core
  0 siblings, 0 replies; 2+ messages in thread
From: matz (Yukihiro Matsumoto) via ruby-core @ 2025-06-05  8:40 UTC (permalink / raw)
  To: ruby-core; +Cc: matz (Yukihiro Matsumoto)

Issue #21382 has been updated by matz (Yukihiro Matsumoto).

Status changed from Open to Closed

Unfortunately, `|` is a binary-or operator too. So we have to be more strict to avoid ambiguity and syntax conflict.

Matz.


----------------------------------------
Bug #21382: Syntax for arguments in || is more strict than arguments in ()
https://bugs.ruby-lang.org/issues/21382#change-113615

* Author: herwin (Herwin W)
* Status: Closed
* ruby -v: ruby 3.4.4 (2025-05-14 revision a38531fd3f) +PRISM [x86_64-linux] (seen in older versions too)
* Backport: 3.2: UNKNOWN, 3.3: UNKNOWN, 3.4: UNKNOWN
----------------------------------------
```ruby
p ->(x, y = x + 1) { x + y }.call(1)
```
This works fine, this gives `y` the value of `x + 1` and prints `3`. The same thing can be used in method definitions.

```ruby
p lambda { |x, y = x + 1| x + y }.call(1)
```
This is the same thing, but results in a parse error (both prism and parse.y):
```
# Prism:
p lambda { |x, y = x + 1| x + y }.call(1)
                    ^ expected the block parameters to end with `|`
                     ^ unexpected '+', ignoring it

# Parse.y:
syntax error, unexpected '+', expecting '|'
```
It works if the default is a single statement (wrapping it in parentheses), so it looks to be purely a grammatical issue
```ruby
p lambda { |x, y = (x + 1)| x + y }.call(1)
```



-- 
https://bugs.ruby-lang.org/
______________________________________________
 ruby-core mailing list -- ruby-core@ml.ruby-lang.org
 To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
 ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2025-06-05  8:40 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-05-29  7:29 [ruby-core:122335] [Ruby Bug#21382] Syntax for arguments in || is more strict than arguments in () herwin (Herwin W) via ruby-core
2025-06-05  8:40 ` [ruby-core:122434] " matz (Yukihiro Matsumoto) via ruby-core

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).