Issue #16261 has been updated by zverok (Victor Shepelev).


@duerst

> What you want to do here is in many other languages done with `zip_with`

I used `zip` only as a simplest way to construct an example. In our current codebase we have a fare share of internal methods defined with two pairs of braces, like `def similar?((word1, word2))`, because this allows us, for example, to say things like (imagine calculating some diffs):

```ruby
diff_pairs.reject(&method(:similar?)).select(&method(:same_paragraph?)).map(&method(:calculate_closeness))
```
In other places, we are still just rely on `map { |foo, bar, baz|`, `select` and so on.

A very small amount of initial data of such chains is produced with `zip`, but even when it is, `zip_with` can't help with select/reject/group_by and other Enumerable methods.

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

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