Module: Boqwij::ArrayExtensions

Defined in:
lib/boqwij/array_extensions.rb

Overview

A set of extensions to the Array class

Instance Method Summary collapse

Instance Method Details

#meanObject

Get the mean of an all-numeric array. Raises a runtime error if non-numeric array is used.



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/boqwij/array_extensions.rb', line 34

def mean
  begin
    raise RuntimeError if self.empty?
    self.each do |value|
      raise RuntimeError unless value.kind_of?(Numeric)
    end
    self.sum.to_f / self.length
  rescue Exception => e
    "The mean could not be calculated for this array"
  end
end

#medianObject

Get the median of an all-numeric array. Raises a runtime error if non-numeric array is used.



48
49
50
51
52
53
54
55
# File 'lib/boqwij/array_extensions.rb', line 48

def median
  begin
    raise RuntimeError if self.empty?
    (self.sort!.size.odd?) ? self[(self.size/2)] : (self[(self.size/2)] + self[((self.size-1)/2)])/2.0
  rescue Exception => e
    "The median could not be calculated for this array"
  end  
end

#productObject

Get the product of an all-numeric array. Raises a runtime error if non-numeric array is used.



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/boqwij/array_extensions.rb', line 20

def product
  begin
    raise RuntimeError if self.empty?
    self.inject do |product, num| 
      raise RuntimeError unless num.kind_of?(Numeric)
      product * num 
    end
  rescue Exception => e
    "The product could not be calculated for this array"
  end
end

#randomObject

Get the first value of a randomized array.



98
99
100
# File 'lib/boqwij/array_extensions.rb', line 98

def random
  self.randomize.first
end

#randomizeObject

Randomize the order of an array.



88
89
90
# File 'lib/boqwij/array_extensions.rb', line 88

def randomize
  self.sort_by { rand }
end

#randomize!Object

Randomize the order of an array and make the change permanent.



93
94
95
# File 'lib/boqwij/array_extensions.rb', line 93

def randomize!
  self.replace(self.randomize)
end

#rangeObject

Get the range of values in an all-numeric array. Raises a runtime error if non-numeric array is used.



59
60
61
62
63
64
65
66
# File 'lib/boqwij/array_extensions.rb', line 59

def range 
  begin
    raise RuntimeError if self.empty?
    (self.max - self.min)
  rescue Exception => e
    "The range could not be calculated for this array"
  end
end

#stdevObject

Get the standard deviation of an all-numeric array. Raises a runtime error if non-numeric array is used.



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/boqwij/array_extensions.rb', line 70

def stdev
  begin
    raise RuntimeError if self.empty?
    # self.each {|i| raise RuntimeError if unless i.kind_of?(Numeric)}
    mean = self.mean
    n = self.length
    if n.zero?
      return nil
    else
      sq_dist = self.inject(0) { |sum, num| sum += (num - mean)**2 } 
      stdev = Math.sqrt(sq_dist/(n-1))
    end
  rescue Exception => e
    "The stddev could not be calculated for this array"
  end
end

#sumObject

Get the sum of an all-numeric array. Raises a runtime error if non-numeric array is used.



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/boqwij/array_extensions.rb', line 6

def sum
  begin
    raise RuntimeError if self.empty?
    self.inject do |sum, num| 
      raise RuntimeError unless num.kind_of?(Numeric)
      sum + num 
    end
  rescue Exception => e
    "The sum could not be calculated for this array"
  end
end