Module: Enumerable

Defined in:
lib/rbkb/extends/enumerable.rb

Instance Method Summary collapse

Instance Method Details

#each_recursive(&block) ⇒ Object



2
3
4
5
6
7
# File 'lib/rbkb/extends/enumerable.rb', line 2

def each_recursive(&block)
  each do |n|
    block.call(n)
    n.each_recursive(&block) if n.is_a?(Enumerable)
  end
end

#mean ⇒ Object



13
14
15
# File 'lib/rbkb/extends/enumerable.rb', line 13

def mean
  sum / length.to_f
end

#sample_variance ⇒ Object



17
18
19
20
21
# File 'lib/rbkb/extends/enumerable.rb', line 17

def sample_variance
  m = mean
  sum = inject(0) { |accum, i| accum + (i - m)**2 }
  sum / (length - 1).to_f
end

#standard_deviation ⇒ Object



23
24
25
# File 'lib/rbkb/extends/enumerable.rb', line 23

def standard_deviation
  Math.sqrt(sample_variance)
end

#sum ⇒ Object



9
10
11
# File 'lib/rbkb/extends/enumerable.rb', line 9

def sum
  inject(0) { |accum, i| accum + i }
end