Issue #16261 has been updated by Dan0042 (Daniel DeLorme).


It's worth pointing out the desired difference with regards to lambdas a bit more explicitly:

```ruby
[1, 2, 3].zip([4, 5, 6]).map(&:+)            # ArgumentError (wrong number of arguments (given 0, expected 1))
[1, 2, 3].zip([4, 5, 6]).each_tuple.map(&:+) # => [5, 7, 9]
```

But in that case it seems to me the behavior you want is the *opposite* of a tuple. Where a tuple is a struct-like set of n elements like `[1, 4]`, what you want here is to destructure that tuple in order to pass each element as an argument of a lambda. So it should be called maybe `each_splat` and the *inverse* operation would be called `each_tuple`.

Or how about something like this based on Enumerator? (hopefully without the hacks)

```ruby
class Enumerator
  def splat
    return to_enum(:splat) unless block_given?
    #each{ |item| yield(*item) } #this doesn't always work
    each{ |first,*rest| yield(first,*rest) } #hacky solution
  end
  def tuple
    return to_enum(:tuple) unless block_given?
    #each{ |*item| yield(item) } #this doesn't always work
    each{ |first,*rest| yield([first,*rest]) } #hacky solution
  end
end

pairs = [10, 20, 30].zip([10, 16, 20])
pairs.each.map(&:to_s)       #=> ["[10, 10]", "[20, 16]", "[30, 20]"]
pairs.each.tuple.map(&:to_s) #=> ["[10, 10]", "[20, 16]", "[30, 20]"]
pairs.each.splat.map(&:to_s) #=> ["10", "14", "1a"]

%i[a b c].each_with_index.map(&:inspect)       # ArgumentError (wrong number of arguments (given 1, expected 0))
%i[a b c].each_with_index.tuple.map(&:inspect) # => ["[:a, 0]", "[:b, 1]", "[:c, 2]"]
%i[a b c].each_with_index.splat.map(&:inspect) # ArgumentError (wrong number of arguments (given 1, expected 0))
```


----------------------------------------
Feature #16261: Enumerable#each_tuple
https://bugs.ruby-lang.org/issues/16261#change-82234

* Author: zverok (Victor Shepelev)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
New method proposal.

Prototype code:
```ruby
module Enumerable
  def each_tuple
    return to_enum(__method__) unless block_given?
    each { |item| yield(*item) } # unpacking possible array into several args
  end
end
```

Supposed documentation/explanation:

> For enumerable with Array items, passes all items in the block provided as a separate arguments. t could be useful if the provided block has lambda semantics, e.g. doesn't unpack arguments automatically. For example:

  ```ruby
files = ["README.md", "LICENSE.txt", "Contributing.md"]
content = [fetch_readme, fetch_license, fetch_contributing] # somehow make a content for the files

files.zip(content).each_tuple(&File.:write) # writes to each file its content
```

> When no block passed, returns enumerator of the tuples:

  ```ruby
[1, 2, 3].zip([4, 5, 6]).each_tuple.map(&:+) # => [5, 7, 9] 
```



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