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
- #_nuggets_original_all? ⇒ Object
- #_nuggets_original_any? ⇒ Object
- #_nuggets_original_max ⇒ Object
- #_nuggets_original_min ⇒ Object
-
#agrep(pattern, distance = 0) ⇒ Object
call-seq: enum.agrep(pattern[, distance]) => anArray enum.agrep(pattern[, distance]) { |obj| … } => anArray.
-
#all?(object = default = Object.new, operator = :===) ⇒ Boolean
call-seq: enum.all?(obj[, operator]) => true or false enum.all? { … } => true or false.
-
#any?(object = default = Object.new, operator = :===) ⇒ Boolean
call-seq: enum.any?(obj[, operator]) => true or false enum.any? { … } => true or false.
-
#max(what = nil) ⇒ Object
call-seq: enum.max(what) => aValue.
-
#max_by(by) ⇒ Object
call-seq: enum.max_by(by) => aValue.
-
#min(what = nil) ⇒ Object
call-seq: enum.min(what) => aValue.
-
#min_by(by) ⇒ Object
call-seq: enum.min_by(by) => aValue.
-
#minmax(meth, what) ⇒ Object
call-seq: enum.minmax(meth, what) => anObject.
-
#minmax_by(meth, by) ⇒ Object
call-seq: enum.minmax_by(meth, by) => aValue.
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_max ⇒ Object
30 |
# File 'lib/nuggets/enumerable/minmax.rb', line 30 alias_method :_nuggets_original_max, :max |
#_nuggets_original_min ⇒ Object
31 |
# File 'lib/nuggets/enumerable/minmax.rb', line 31 alias_method :_nuggets_original_min, :min |
#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 toagrep -k) -
Only works with string elements in enum. (Calls
to_son 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 = :===) ⇒ 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 :===.
40 41 42 43 44 |
# File 'lib/nuggets/enumerable/all_any_extended.rb', line 40 def all?(object = default = Object.new, operator = :===) _nuggets_original_all?(&block_given? ? _block_for_all_any_extended(object, default, operator) { |*a| yield(*a) } : _block_for_all_any_extended(object, default, operator)) end |
#any?(object = default = Object.new, operator = :===) ⇒ 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 :===.
53 54 55 56 57 |
# File 'lib/nuggets/enumerable/all_any_extended.rb', line 53 def any?(object = default = Object.new, operator = :===) _nuggets_original_any?(&block_given? ? _block_for_all_any_extended(object, default, operator) { |*a| yield(*a) } : _block_for_all_any_extended(object, default, operator)) 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.
85 86 87 88 |
# File 'lib/nuggets/enumerable/minmax.rb', line 85 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.
48 49 50 |
# File 'lib/nuggets/enumerable/minmax.rb', line 48 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.
95 96 97 98 |
# File 'lib/nuggets/enumerable/minmax.rb', line 95 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.
56 57 58 |
# File 'lib/nuggets/enumerable/minmax.rb', line 56 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
67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/nuggets/enumerable/minmax.rb', line 67 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).
39 40 41 42 |
# File 'lib/nuggets/enumerable/minmax.rb', line 39 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 |