[ruby-core:94926] [Ruby master Feature#16166] Remove exceptional treatment of *foo when it is the sole block parameter
From:
mame@...
Date:
2019-09-13 10:16:48 UTC
List:
ruby-core #94926
Issue #16166 has been updated by mame (Yusuke Endoh).
I agree that Ruby's arguments are insanely complex. In the basic case, `"a"` and `["a"]` are distinguished.
```
p instance_exec("a") {|foo| foo } #=> "a"
p instance_exec(["a"]){|foo| foo } #=> ["a"]
p instance_exec("a") {|*foo| foo } #=> ["a"]
p instance_exec(["a"]){|*foo| foo } #=> [["a"]]
```
In *some* cases, they are not distinguished.
```
p instance_exec("a") {|foo, bar| foo } #=> "a"
p instance_exec(["a"]){|foo, bar| foo } #=> "a"
p instance_exec(["a"]){|*foo, **bar| foo } #=> ["a"]
p instance_exec("a") {|*foo, **bar| foo } #=> ["a"]
```
The rule is fairly complex or even inconsistent. I cannot understand [the condition](https://github.com/ruby/ruby/blob/5f5aca1b5fb95013f5b805f74ead4cfa143dc1d8/vm_args.c#L765-L769).
I have no opinion which case `|*foo|` should belong to. (I personally hope that `|*foo, **bar|` belongs to the same case as `|*foo|` because keywords are separated from positional arguments.)
Anyway, I don't think that it is a good idea to change the behavior just because it is inconsistent. We need an evidence that the behavior actually confuses many people, at least.
----------------------------------------
Feature #16166: Remove exceptional treatment of *foo when it is the sole block parameter
https://bugs.ruby-lang.org/issues/16166#change-81542
* Author: sawa (Tsuyoshi Sawada)
* Status: Open
* Priority: Normal
* Assignee:
* Target version:
----------------------------------------
In the parameter signature of a code block for a method that is not involved in method definition or creation of lambda objects, two types of arguments `["a"]` and `"a"` are neutralized:
```ruby
instance_exec(["a"]){|foo, bar| foo} # => "a"
instance_exec("a"){|foo, bar| foo} # => "a"
instance_exec(["a"]){|*foo, **bar| foo} # => ["a"]
instance_exec("a"){|*foo, **bar| foo} # => ["a"]
```
This is the same behavior as with assignment constructions:
```ruby
foo, bar = ["a"]; foo # => "a"
foo, bar = "a"; foo # => "a"
*foo = ["a"]; foo # => ["a"]
*foo = "a"; foo # => ["a"]
```
And it contrasts with constructions involved in method definition or creation of lambda objects, where the distinction is preserved:
```ruby
lambda{|foo| foo}.call(["a"]) # => ["a"]
lambda{|foo| foo}.call("a") # => "a"
->(foo){foo}.call(["a"]) # => ["a"]
->(foo){foo}.call("a") # => "a"
lambda{|*foo| foo}.call(["a"]) # => [["a"]]
lambda{|*foo| foo}.call("a") # => ["a"]
->(*foo){foo}.call(["a"]) # => [["a"]]
->(*foo){foo}.call("a") # => ["a"]
```
However, when `*foo` is the sole parameter of a code block for a method that is not involved in method definition or creation of lambda objects, `["a"]` and `"a"` are not neutralized:
```ruby
instance_exec(["a"]){|*foo| foo} # => [["a"]]
instance_exec("a"){|*foo| foo} # => ["a"]
```
behaving in contrast to assignment constructions, and rather on a par with constructions involved in method definition or creation of lambda objects.
Particularly, existence or absence of another parameter `**bar` entirely changes what `foo` refers to:
```ruby
instance_exec(["a"]){|*foo| foo} # => [["a"]]
instance_exec(["a"]){|*foo, **bar| foo} # => ["a"]
```
I find this behavior inconsistent and confusing. I would like to request to remove this exceptional treatment of splatted parameter `*foo` when it is the sole parameter in a code block. I request this behavior:
```ruby
instance_exec(["a"]){|*foo| foo} # => ["a"]
```
--
https://bugs.ruby-lang.org/
Unsubscribe: <mailto:ruby-core-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>