Sha256: 490b2d37bc1a0e00aa2d48363634daec6ce22bbdd4b8a056204daa47dc3670b9

Contents?: true

Size: 870 Bytes

Versions: 3

Compression:

Stored size: 870 Bytes

Contents

# frozen_string_literal: true

class Array
  autoload :HashBuilder, 'darthjee/core_ext/array/hash_builder'

  def mapk(*keys)
    keys.inject(self) do |enum, key|
      enum.map { |hash| hash[key] }
    end
  end

  def procedural_join(mapper = proc(&:to_s))
    return '' if empty?
    list = dup
    prev = first
    list[0] = mapper.call(prev).to_s

    list.inject do |string, val|
      j = yield(prev, val) if block_given?
      "#{string}#{j}#{mapper.call(val)}".tap do
        prev = val
      end
    end
  end

  def chain_map(*methods, &block)
    result = methods.inject(self) do |array, method|
      array.map(&method)
    end

    return result unless block_given?
    result.map(&block)
  end

  def as_hash(keys)
    Array::HashBuilder.new(self, keys).build
  end

  def random
    self[rand(size)]
  end

  def random!
    slice!(rand(size))
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
darthjee-core_ext-1.6.2 lib/darthjee/core_ext/array.rb
darthjee-core_ext-1.6.1 lib/darthjee/core_ext/array.rb
darthjee-core_ext-1.6.0 lib/darthjee/core_ext/array.rb