module Nuggets::Array::ModeMixin

Public Class Methods

included(base) click to toggle source
# File lib/nuggets/array/mode_mixin.rb, line 33
def self.included(base)
  base.send :include, Nuggets::Array::HistogramMixin
end

Public Instance Methods

mode → anObject click to toggle source
mode(+true+) → anArray

Returns the mode of the values in array (via histogram).

If parameter true is passed, an Array of all modes is returned.

# File lib/nuggets/array/mode_mixin.rb, line 45
def mode(all = false, &block)
  hist, modes = histogram(&block), []
  freq = hist.values.max

  hist.each { |key, value|
    if value == freq
      modes << key
      break unless all
    end
  }

  all ? modes : modes.first
end
modes → anArray click to toggle source

Returns an Array of all modes of the values in array (see mode).

# File lib/nuggets/array/mode_mixin.rb, line 63
def modes(&block)
  mode(true, &block)
end