Project

General

Profile

Feature #15922

Enumerable#partition(pattern)

Added by kachick (Kenichi Kamiya) 6 months ago.

Status:
Open
Priority:
Normal
Assignee:
-
Target version:
-
[ruby-core:93128]

Description

I would like to use partition with pattern argument for consistency.

module Enumerable
  alias_method :original_partition, :partition

  def partition(*args, &block)
    case args.size
    when 1
      pattern = args.first
      original_partition do |element|
        pattern === element
      end
    when 0
      original_partition(&block)
    else
      raise ArgumentError
    end
  end
end

[1, 2, 3.3, 4, 5.5].partition(Integer)
#=> => [[1, 2, 4], [3.3, 5.5]]

Also available in: Atom PDF