Sha256: d7a9ba19771d093941830f506e0136b4f94e048912adab9a2168c2cb2ed1f617

Contents?: true

Size: 1 KB

Versions: 33

Compression:

Stored size: 1 KB

Contents

require 'bigdecimal'
require 'bigdecimal/util'

module FeCoreExt::CoreExt
end

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

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

  # remove, remove_at, remove_if はこちらのものを使わせてもらっています。
  # https://gist.github.com/nekoTheShadow/2de8a324f445bdde8a67
  def remove(val)
    # ブロックが渡されており、かつvalと等しい要素が見つからなかった場合
    return yield if block_given? && !self.include?(val)

    temp = self.dup
    temp.delete(val)
    temp
  end

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

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

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

end

class Array
  include FeCoreExt::CoreExt::Array
end

Version data entries

33 entries across 33 versions & 1 rubygems

Version Path
fe_core_ext-0.26.0 lib/fe_core_ext/core_ext/array.rb
fe_core_ext-0.25.0 lib/fe_core_ext/core_ext/array.rb
fe_core_ext-0.24.0 lib/fe_core_ext/core_ext/array.rb
fe_core_ext-0.23.0 lib/fe_core_ext/core_ext/array.rb
fe_core_ext-0.22.0 lib/fe_core_ext/core_ext/array.rb
fe_core_ext-0.21.0 lib/fe_core_ext/core_ext/array.rb
fe_core_ext-0.20.0 lib/fe_core_ext/core_ext/array.rb
fe_core_ext-0.19.0 lib/fe_core_ext/core_ext/array.rb
fe_core_ext-0.18.1 lib/fe_core_ext/core_ext/array.rb
fe_core_ext-0.18.0 lib/fe_core_ext/core_ext/array.rb
fe_core_ext-0.17.3 lib/fe_core_ext/core_ext/array.rb
fe_core_ext-0.17.2 lib/fe_core_ext/core_ext/array.rb
fe_core_ext-0.17.1 lib/fe_core_ext/core_ext/array.rb
fe_core_ext-0.17.0 lib/fe_core_ext/core_ext/array.rb
fe_core_ext-0.16.1 lib/fe_core_ext/core_ext/array.rb
fe_core_ext-0.16.0 lib/fe_core_ext/core_ext/array.rb
fe_core_ext-0.15.0 lib/fe_core_ext/core_ext/array.rb
fe_core_ext-0.14.1 lib/fe_core_ext/core_ext/array.rb
fe_core_ext-0.14.0 lib/fe_core_ext/core_ext/array.rb
fe_core_ext-0.13.1 lib/fe_core_ext/core_ext/array.rb