module Enumerable

Public Instance Methods

_nuggets_original_max(what = nil, &block)
Alias for: max
_nuggets_original_max_by(by = nil, &block)
Alias for: max_by
_nuggets_original_min(what = nil, &block)
Alias for: min
_nuggets_original_min_by(by = nil, &block)
Alias for: min_by
_nuggets_original_minmax(meth = nil, what = nil, &block)
Alias for: minmax
_nuggets_original_minmax_by(meth = nil, by = nil, &block)
Alias for: minmax_by
agrep(pattern[, distance]) → anArray click to toggle source
agrep(pattern[, distance]) { |element| ... } → enum

Returns an array of all elements in enum for which pattern approximately matches element (see Amatch::Levenshtein#search). If the optional block is supplied, each matching element is passed to it, and the enum itself is returned.

LIMITATIONS:

  • Only strings are allowed as pattern. Regular expressions are reverted to their respective source. (Equivalent to agrep -k)

  • Only works with string elements in enum. (Calls to_s on each element)

  • The cost for individual error types (substitution, insertion, deletion) cannot be adjusted.

# File lib/nuggets/enumerable/agrep.rb, line 56
def agrep(pattern, distance = 0)
  pattern = pattern.source if pattern.is_a?(::Regexp)

  am = ::Amatch::Levenshtein.new(pattern)
  ma = lambda { |i| am.search(i.to_s) <= distance }

  block_given? ? each { |i| yield i if ma[i] } : select(&ma)
end
max(what) → aValue click to toggle source

Maximum minmax. If what is omitted, or nil, the original #max is called.

# File lib/nuggets/enumerable/minmax.rb, line 95
def max(what = nil, &block)
  what ? minmax(:max, what) : _nuggets_original_max(&block)
end
Also aliased as: _nuggets_original_max
max_by(by) → aValue click to toggle source

Maximum minmax_by.

# File lib/nuggets/enumerable/minmax.rb, line 54
def max_by(by = nil, &block)
  by.nil? || by.is_a?(::Numeric) ?
    _nuggets_original_max_by(by, &block) : minmax_by(:max, by)
end
Also aliased as: _nuggets_original_max_by
min(what) → aValue click to toggle source

Minimum minmax. If what is omitted, or nil, the original #min is called.

# File lib/nuggets/enumerable/minmax.rb, line 104
def min(what = nil, &block)
  what ? minmax(:min, what) : _nuggets_original_min(&block)
end
Also aliased as: _nuggets_original_min
min_by(by) → aValue click to toggle source

Minimum minmax_by.

# File lib/nuggets/enumerable/minmax.rb, line 63
def min_by(by = nil, &block)
  by.nil? || by.is_a?(::Numeric) ?
    _nuggets_original_min_by(by, &block) : minmax_by(:min, by)
end
Also aliased as: _nuggets_original_min_by
minmax(meth, what) → anObject click to toggle source

Finds the minmax_by according to what and returns that “what”.

Example:

%w[a bcd ef].max(:length)  #=> 3
# File lib/nuggets/enumerable/minmax.rb, line 75
def minmax(meth = nil, what = nil, &block)
  return _nuggets_original_minmax(&block) unless meth

  #m = minmax_by(meth, what)
  #what.is_a?(Proc) ? what[m] : m.send(what)

  _what = what.is_a?(::Proc) ? what : lambda { |i| i.send(what) }
  map { |i| _what[i] }.send(meth)

  # Benchmark (:max, :length; enum.size = 20, N = 100_000):
  #
  # max_by(:length).length   7.920000   0.890000   8.810000 (  8.991915)
  # map(:length).max         4.800000   0.600000   5.400000 (  5.418114)
end
Also aliased as: _nuggets_original_minmax
minmax_by(meth, by) → aValue click to toggle source

Finds the maximum/minimum (or whatever meth is) value in enum according to by (which may be a symbol/string that is sent to each value, or a proc that receives each value as parameter).

# File lib/nuggets/enumerable/minmax.rb, line 43
def minmax_by(meth = nil, by = nil, &block)
  return _nuggets_original_minmax_by(&block) unless meth

  _by = by.is_a?(::Proc) ? by : lambda { |i| i.send(by) }
  send(meth) { |a, b| _by[a] <=> _by[b] }
end
Also aliased as: _nuggets_original_minmax_by