Project

General

Profile

Actions

Feature #16261

closed

Enumerable#each_splat and Enumerator#splat

Added by zverok (Victor Shepelev) over 1 year ago. Updated over 1 year ago.

Status:
Rejected
Priority:
Normal
Assignee:
-
Target version:
-
[ruby-core:95419]

Description

UPD: After discussion in comments, method names changed to "splat"-based.

New methods proposal.

Prototype code:

module Enumerable
  def each_splat
    return to_enum(__method__) unless block_given?
    each_entry { |item| yield(*item) } # unpacking possible array into several args
  end
end

class Enumerator
  def splat
    return to_enum(:splat) unless block_given?
    each_entry { |item| yield(*item) }
  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:

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_splat(&File.:write) # writes to each file its content

When no block passed, returns enumerator of the tuples:

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

Related issues

Related to Ruby master - Feature #4539: Array#zip_withAssignedmatz (Yukihiro Matsumoto)Actions
Related to Ruby master - Feature #5044: #zip with block return mapped resultsRejectedmatz (Yukihiro Matsumoto)Actions
Actions

Also available in: Atom PDF