class Array

Public Instance Methods

average()
Alias for: avg
avg() click to toggle source
# File lib/theusual/array.rb, line 10
def avg
  numerics?
  return Float::NAN if empty?

  map(&:to_f).sum / count
end
Also aliased as: average, mean
compact(modifier = nil) click to toggle source

Misc Operations #####

# File lib/theusual/array.rb, line 49
def compact(modifier = nil)
  falsy = modifier == :falsy
  blanks = falsy || modifier == :blanks

  reject do |v|
    isblank = blanks && v.respond_to?(:empty?) && v.empty?
    isfalsy = falsy && (v == 0)

    !v || isblank || isfalsy
  end
end
compact!(modifier = nil) click to toggle source
# File lib/theusual/array.rb, line 61
def compact!(modifier = nil)
  res = compact(modifier)
  clear

  # TODO: is there a better way than shift/reverse?
  res.each {|x| unshift x}
  reverse!
end
grepv(regex) click to toggle source

String Operations #####

# File lib/theusual/array.rb, line 34
def grepv regex
  reject { |elem| elem =~ regex }
end
gsub(regex, replacement) click to toggle source
# File lib/theusual/array.rb, line 39
def gsub(regex, replacement)
  map { |string| string.gsub(regex, replacement) }
end
gsub!(regex, replacement) click to toggle source
# File lib/theusual/array.rb, line 43
def gsub!(regex, replacement)
  map! { |string| string.gsub(regex, replacement) }
end
mean()
Alias for: avg
standard_deviation()
Alias for: std
std() click to toggle source
# File lib/theusual/array.rb, line 20
def std
  numerics?
  return Float::NAN if empty?

  mean = avg

  Math.sqrt(
      map { |sample| (mean - sample.to_f) ** 2 }.reduce(:+) / count.to_f
  )
end
Also aliased as: standard_deviation
sum() click to toggle source

Numerical Operations #####

# File lib/theusual/array.rb, line 3
def sum
  numerics?
  inject 0, &:+
end
Also aliased as: total
total()
Alias for: sum

Private Instance Methods

numerics?() click to toggle source
# File lib/theusual/array.rb, line 73
def numerics?
  # make sure values are all Numeric or numbers within strings
  map do |x|
    Float(x)
  end
end