Module: FeCoreExt::CoreExt::Array

Included in:
Array
Defined in:
lib/fe_core_ext/core_ext/array.rb

Instance Method Summary collapse

Instance Method Details

#meanObject



16
17
18
19
# File 'lib/fe_core_ext/core_ext/array.rb', line 16

def mean
  return if empty?
  sum.to_d / size
end

#medianObject



9
10
11
12
13
14
# File 'lib/fe_core_ext/core_ext/array.rb', line 9

def median
  return if empty?
  sorted = sort
  len = sorted.length
  (sorted[(len - 1) / 2] + sorted[len / 2]) / 2.0
end

#remove(val) ⇒ Object

remove, remove_at, remove_if はこちらのものを使わせてもらっています。 gist.github.com/nekoTheShadow/2de8a324f445bdde8a67



23
24
25
26
27
28
29
30
# File 'lib/fe_core_ext/core_ext/array.rb', line 23

def remove(val)
  # ブロックが渡されており、かつvalと等しい要素が見つからなかった場合
  return yield if block_given? && !include?(val)

  temp = dup
  temp.delete(val)
  temp
end

#remove_at(pos) ⇒ Object



32
33
34
35
36
# File 'lib/fe_core_ext/core_ext/array.rb', line 32

def remove_at(pos)
  temp = dup
  temp.delete_at(pos)
  temp
end

#remove_ifObject



38
39
40
41
42
43
44
45
# File 'lib/fe_core_ext/core_ext/array.rb', line 38

def remove_if
  temp = dup
  # ブロックを持たない場合
  return temp.delete_if unless block_given?

  each { |val| temp.delete(val) if yield(val) }
  temp
end