module Nuggets::Array::ModeMixin
Public Class Methods
included(base)
click to toggle source
# File lib/nuggets/array/mode_mixin.rb 33 def self.included(base) 34 base.send :include, Nuggets::Array::HistogramMixin 35 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 45 def mode(all = false, &block) 46 hist, modes = histogram(&block), [] 47 freq = hist.values.max 48 49 hist.each { |key, value| 50 if value == freq 51 modes << key 52 break unless all 53 end 54 } 55 56 all ? modes : modes.first 57 end