Sha256: 78110df958bcdc831e806bb930a65578380b78b2699df24df987e75823f5c7ca

Contents?: true

Size: 1021 Bytes

Versions: 2

Compression:

Stored size: 1021 Bytes

Contents

module Option
  module Enumerable
    include ::Enumerable

    def to_ary
      to_a
    end

    def flatten
      from_array to_ary.flatten
    end

    def juxt(*methods)
      map { |v| methods.map { |m| v.send(m) } }
    end

    def map_through(*methods)
      map &methods.reduce(->(x){x}){ |acc, m| ->(x) { acc.call(x).send(m) } }
    end

    def map
      from_array super
    end
    alias_method :collect, :map
    alias_method :collect_concat, :map

    def flat_map(&block)
      map(&block).flatten
    end

    def detect
      from_value super
    end
    alias_method :find, :detect

    def select
      from_array super
    end
    alias_method :find_all, :select

    def grep(value)
      from_array super
    end

    def reject(*args, &block)
      from_array to_a.reject(*args, &block)
    end

    def reduce(*args, &block)
      if none? && (args.size < 1 || args.size < 2 && block.nil?)
        raise ValueOfNoneError
      end
      super
    end
    alias_method :inject, :reduce
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
optional-0.0.6 lib/optional/option/enumerable.rb
optional-0.0.5 lib/optional/option/enumerable.rb