Issue #16261 has been updated by duerst (Martin D=FCrst).


Dan0042 (Daniel DeLorme) wrote:
> 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 numbe=
r of arguments (given 0, expected 1))
> [1, 2, 3].zip([4, 5, 6]).each_tuple.map(&:+) # =3D> [5, 7, 9]
> ```

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

```ruby
[1, 2, 3].zip_with([4, 5, 6], :+) # =3D> [5, 7, 9]
```

There is already an issue for this, issue #4539, which is open and waits fo=
r Matz's approval.

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

* 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 ar=
gs
  end
end
```

Supposed documentation/explanation:

> For enumerable with Array items, passes all items in the block provided a=
s 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 =3D ["README.md", "LICENSE.txt", "Contributing.md"]
content =3D [fetch_readme, fetch_license, fetch_contributing] # somehow mak=
e a content for the files

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

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

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

```



-- =

https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=3Dunsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>