Module: Nuggets::Array::ModeMixin

Included in:
Array
Defined in:
lib/nuggets/array/mode_mixin.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



33
34
35
# File 'lib/nuggets/array/mode_mixin.rb', line 33

def self.included(base)
  base.send :include, Nuggets::Array::HistogramMixin
end

Instance Method Details

#mode(all = false, &block) ⇒ Object

call-seq:

array.mode => anObject
array.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.



45
46
47
48
49
50
51
52
53
54
55
56
57
# 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(&block) ⇒ Object

call-seq:

array.modes => anArray

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



63
64
65
# File 'lib/nuggets/array/mode_mixin.rb', line 63

def modes(&block)
  mode(true, &block)
end