Module: Enumerable

Defined in:
lib/nuggets/enumerable/minmax.rb,
lib/nuggets/enumerable/agrep.rb,
lib/nuggets/enumerable/all_any_extended.rb

Overview

#

A component of ruby-nuggets, some extensions to the Ruby programming # language. #

#

Copyright © 2007-2008 Jens Wille #

#

Authors: #

Jens Wille <jens.wille@uni-koeln.de>                                    #
                                                                        #

ruby-nuggets is free software; you can redistribute it and/or modify it # under the terms of the GNU General Public License as published by the Free # Software Foundation; either version 3 of the License, or (at your option) # any later version. #

#

ruby-nuggets is distributed in the hope that it will be useful, but WITHOUT # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for # more details. #

#

You should have received a copy of the GNU General Public License along # with ruby-nuggets. If not, see <www.gnu.org/licenses/>. #

#

++

Instance Method Summary collapse

Instance Method Details

#_nuggets_original_all?Object



30
# File 'lib/nuggets/enumerable/all_any_extended.rb', line 30

alias_method :_nuggets_original_all?, :all?

#_nuggets_original_any?Object



31
# File 'lib/nuggets/enumerable/all_any_extended.rb', line 31

alias_method :_nuggets_original_any?, :any?

#_nuggets_original_maxObject



35
# File 'lib/nuggets/enumerable/minmax.rb', line 35

alias_method :_nuggets_original_max, :max

#_nuggets_original_max_byObject



32
# File 'lib/nuggets/enumerable/minmax.rb', line 32

alias_method :_nuggets_original_max_by,    :max_by

#_nuggets_original_minObject



36
# File 'lib/nuggets/enumerable/minmax.rb', line 36

alias_method :_nuggets_original_min, :min

#_nuggets_original_min_byObject



31
# File 'lib/nuggets/enumerable/minmax.rb', line 31

alias_method :_nuggets_original_min_by,    :min_by

#_nuggets_original_minmaxObject



33
# File 'lib/nuggets/enumerable/minmax.rb', line 33

alias_method :_nuggets_original_minmax,    :minmax

#_nuggets_original_minmax_byObject



30
# File 'lib/nuggets/enumerable/minmax.rb', line 30

alias_method :_nuggets_original_minmax_by, :minmax_by

#agrep(pattern, distance = 0) ⇒ Object

call-seq:

enum.agrep(pattern[, distance]) => anArray
enum.agrep(pattern[, distance]) { |obj| ... } => anArray

Returns an array of every element 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 block‘s result is stored in the output array.

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.



57
58
59
60
61
62
63
64
65
66
# File 'lib/nuggets/enumerable/agrep.rb', line 57

def agrep(pattern, distance = 0)
  raise 'Amatch not available!' unless defined?(Amatch)

  pattern = pattern.source if pattern.is_a?(Regexp)

  amatch  = Amatch::Levenshtein.new(pattern)
  matches = select { |obj| amatch.search(obj.to_s) <= distance }

  block_given? ? matches.map { |match| yield match } : matches
end

#all?(object = default = Object.new, operator = :===, &block) ⇒ Boolean

call-seq:

enum.all?(obj[, operator]) => true or false
enum.all? { ... } => true or false

Adds the ability to pass an object instead of a block, which will then be tested against each item in enum according to operator, defaulting to :===.

Returns:

  • (Boolean)


40
41
42
# File 'lib/nuggets/enumerable/all_any_extended.rb', line 40

def all?(object = default = Object.new, operator = :===, &block)
  _nuggets_original_all?(&_block_for_all_any_extended(object, default, operator, &block))
end

#any?(object = default = Object.new, operator = :===, &block) ⇒ Boolean

call-seq:

enum.any?(obj[, operator]) => true or false
enum.any? { ... } => true or false

Adds the ability to pass an object instead of a block, which will then be tested against each item in enum according to operator, defaulting to :===.

Returns:

  • (Boolean)


51
52
53
# File 'lib/nuggets/enumerable/all_any_extended.rb', line 51

def any?(object = default = Object.new, operator = :===, &block)
  _nuggets_original_any?(&_block_for_all_any_extended(object, default, operator, &block))
end

#max(what = nil) ⇒ Object

call-seq:

enum.max(what) => aValue

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



90
91
92
93
# File 'lib/nuggets/enumerable/minmax.rb', line 90

def max(what = nil)
  what ? minmax(:max, what) : block_given? ?
    _nuggets_original_max { |*a| yield(*a) } : _nuggets_original_max
end

#max_by(by) ⇒ Object

call-seq:

enum.max_by(by) => aValue

Maximum #minmax_by.



53
54
55
# File 'lib/nuggets/enumerable/minmax.rb', line 53

def max_by(by)
  minmax_by(:max, by)
end

#min(what = nil) ⇒ Object

call-seq:

enum.min(what) => aValue

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



100
101
102
103
# File 'lib/nuggets/enumerable/minmax.rb', line 100

def min(what = nil)
  what ? minmax(:min, what) : block_given? ?
    _nuggets_original_min { |*a| yield(*a) } : _nuggets_original_min
end

#min_by(by) ⇒ Object

call-seq:

enum.min_by(by) => aValue

Minimum #minmax_by.



61
62
63
# File 'lib/nuggets/enumerable/minmax.rb', line 61

def min_by(by)
  minmax_by(:min, by)
end

#minmax(meth, what) ⇒ Object

call-seq:

enum.minmax(meth, what) => anObject

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

Example:

%w[a bcd ef].max(:length)  #=> 3


72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/nuggets/enumerable/minmax.rb', line 72

def minmax(meth, what)
  #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

#minmax_by(meth, by) ⇒ Object

call-seq:

enum.minmax_by(meth, by) => aValue

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).



44
45
46
47
# File 'lib/nuggets/enumerable/minmax.rb', line 44

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