Module: ArrayHelper
- Included in:
- Array
- Defined in:
- lib/rubyhelper/arrayhelper.rb
Instance Method Summary collapse
-
#average ⇒ Object
Use the sum and divide by the size of the array.
-
#averagef ⇒ Object
Same than average but use to_f instead of to_i.
-
#compacti ⇒ Object
The Same as compact but remove empty string too.
- #compacti! ⇒ Object
-
#joini(sep) ⇒ Object
This function return a pretty good string.
-
#maxs(n = 1) ⇒ Object
get the n higher values of the array == Errors ArgumentError : if n in not an integer == Params n: (Integer) number of elements.
-
#mins(n = 1) ⇒ Object
get the n lower values of the array == Errors ArgumentError : if n in not an integer == Params n: (Integer) number of elements.
-
#sum ⇒ Object
Do the sum of an array of integer.
-
#sumf ⇒ Object
lke sum by with a to_f instead of to_i.
Instance Method Details
#average ⇒ Object
Use the sum and divide by the size of the array. 0 if no element.
38 39 40 |
# File 'lib/rubyhelper/arrayhelper.rb', line 38 def average return (self.size > 0) ? (self.sum / self.size) : (0) end |
#averagef ⇒ Object
Same than average but use to_f instead of to_i
43 44 45 |
# File 'lib/rubyhelper/arrayhelper.rb', line 43 def averagef return (self.size > 0) ? (self.sumf / self.size.to_f) : (0.0) end |
#compacti ⇒ Object
The Same as compact but remove empty string too
72 73 74 |
# File 'lib/rubyhelper/arrayhelper.rb', line 72 def compacti return self.map{|e| e = nil if e == "" ; e}.compact end |
#compacti! ⇒ Object
75 76 77 |
# File 'lib/rubyhelper/arrayhelper.rb', line 75 def compacti! return self.replace(self.compacti) end |
#joini(sep) ⇒ Object
This function return a pretty good string. Like join but with an array You can use an array as separator : [“,”, “ ”] will successively ‘,’ and ‘ ’ Exemple : [“a”, “b”, “c”].joini([“x”, “y”]) => “axbyc”
Errors
ArgumentError : if sep in not an array
Params
sep: (Array) array of separator
Returns
str: (String) string joined
14 15 16 17 18 19 20 21 22 23 |
# File 'lib/rubyhelper/arrayhelper.rb', line 14 def joini sep raise ArgumentError, 'Argument is not an array' unless sep.is_a? Array str = String.new i = 0 self.each do |e| str = str + e.to_s + sep[i % sep.size].to_s i += 1 end return str[0..-2] end |
#maxs(n = 1) ⇒ Object
get the n higher values of the array
Errors
ArgumentError : if n in not an integer
Params
n: (Integer) number of elements
52 53 54 55 56 57 |
# File 'lib/rubyhelper/arrayhelper.rb', line 52 def maxs(n=1) raise ArgumentError, 'Argument is not an integer' unless n.is_a? Integer n = 1 if n < 1 n = self.size if n > self.size return Array(self.sort[(-n)..(-1)]) end |
#mins(n = 1) ⇒ Object
get the n lower values of the array
Errors
ArgumentError : if n in not an integer
Params
n: (Integer) number of elements
64 65 66 67 68 69 |
# File 'lib/rubyhelper/arrayhelper.rb', line 64 def mins(n=1) raise ArgumentError, 'Argument is not an integer' unless n.is_a? Integer n = 1 if n < 1 n = self.size if n > self.size return Array(self.sort[0..(n-1)]) end |
#sum ⇒ Object
Do the sum of an array of integer. if there is not integer, it will do a to_s.to_i of the element to try find an integer in the element. else, replace by a simple 0
28 29 30 |
# File 'lib/rubyhelper/arrayhelper.rb', line 28 def sum return (self.size > 0) ? (self.map{|e| e.to_s.to_i}.reduce(:+)) : (0) end |
#sumf ⇒ Object
lke sum by with a to_f instead of to_i
33 34 35 |
# File 'lib/rubyhelper/arrayhelper.rb', line 33 def sumf return (self.size > 0) ? (self.map{|e| e.to_s.to_f}.reduce(:+)) : (0.0) end |