Module: ArrayHelper
- Included in:
- Array
- Defined in:
- lib/rubyhelper/array.rb
Instance Method Summary collapse
-
#average ⇒ Integer
Use the #sum and divide by the size of the array.
-
#averagef ⇒ Float
Same than #average but use to_f instead of to_i.
-
#compacti ⇒ Array
Same as #compact but remove empty string too see #compact.
-
#compacti! ⇒ Array
see #compacti.
-
#do(method_to_exec) ⇒ Array
try to use the method (param method_to_exec) on every element of the array.
-
#do!(method_to_exec) ⇒ Array
see #do.
-
#joini(sep) ⇒ String
This function return a pretty good string.
-
#maxs(n = 1) ⇒ Object
get the n higher values of the array.
-
#mins(n = 1) ⇒ Object
get the n lower values of the array.
-
#mirror ⇒ Array
examples : [:a, :b, :c, 1, 2, 3] => [[:a, 1], [:b, 2], [:c, 3]] examples : [:a, :b, :c, :d, 1, 2, 3] => [[:a, 1], [:b, 2], [:c, 3], [:d, nil]] examples : [:a, :b, :c, 1, 2, 3, 4] => [[:a, 2], [:b, 3], [:c, 4], [1, nil]] The array will be transformed into an array of couple ([x,y]) with [0e, N/2e] where N/2e is the element at size/2.
-
#split! ⇒ Array
see #split.
-
#strip ⇒ Array
try to use the method split on every element of the array.
-
#sum(toadd = nil) ⇒ Integer, Array
Do the sum of an array of integer.
-
#sumf(toadd = nil) ⇒ Float, Array
like #sum by with a to_f instead of to_i.
Instance Method Details
#average ⇒ Integer
Use the #sum and divide by the size of the array.
58 59 60 |
# File 'lib/rubyhelper/array.rb', line 58 def average return (self.size > 0) ? (self.sum.to_f / self.size).round : (0) end |
#averagef ⇒ Float
Same than #average but use to_f instead of to_i
65 66 67 |
# File 'lib/rubyhelper/array.rb', line 65 def averagef return (self.size > 0) ? (self.sumf / self.size.to_f) : (0.0) end |
#compacti ⇒ Array
Same as #compact but remove empty string too see #compact
98 99 100 |
# File 'lib/rubyhelper/array.rb', line 98 def compacti return self.map{|e| e == "" ? nil : e}.compact end |
#compacti! ⇒ Array
see #compacti
105 106 107 |
# File 'lib/rubyhelper/array.rb', line 105 def compacti! return self.replace(self.compacti) end |
#do(method_to_exec) ⇒ Array
try to use the method (param method_to_exec) on every element of the array
140 141 142 143 |
# File 'lib/rubyhelper/array.rb', line 140 def do(method_to_exec) raise ArgumentError, "method_to_exec is not a valid Method" unless method_to_exec.is_a? String or method_to_exec.is_a? Symbol self.map{|e| ((e.respond_to? method_to_exec) ? (e.public_send(method_to_exec)) : (e)) } end |
#do!(method_to_exec) ⇒ Array
see #do
149 150 151 |
# File 'lib/rubyhelper/array.rb', line 149 def do!(method_to_exec) self.replace(self.do(method_to_exec)) end |
#joini(sep) ⇒ String
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”
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/rubyhelper/array.rb', line 12 def joini sep if 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] elsif sep.is_a? String return self.join(sep) else raise ArgumentError, 'Argument is not an array or a string' end end |
#maxs(n = 1) ⇒ Object
get the n higher values of the array
74 75 76 77 78 79 |
# File 'lib/rubyhelper/array.rb', line 74 def maxs(n=1) raise ArgumentError, 'Argument is not an integer' unless n.is_a? Integer raise ArgumentError, 'Argument is lesser than 1' unless 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
86 87 88 89 90 91 92 |
# File 'lib/rubyhelper/array.rb', line 86 def mins(n=1) raise ArgumentError, 'Argument is not an integer' unless n.is_a? Integer raise ArgumentError, 'Argument is lesser than 1' unless n >= 1 n = 1 if n < 1 n = self.size if n > self.size return Array(self.sort[0..(n-1)]) end |
#mirror ⇒ Array
examples : [:a, :b, :c, 1, 2, 3] => [[:a, 1], [:b, 2], [:c, 3]] examples : [:a, :b, :c, :d, 1, 2, 3] => [[:a, 1], [:b, 2], [:c, 3], [:d, nil]] examples : [:a, :b, :c, 1, 2, 3, 4] => [[:a, 2], [:b, 3], [:c, 4], [1, nil]] The array will be transformed into an array of couple ([x,y]) with [0e, N/2e] where N/2e is the element at size/2. If the array have an odd number of elements, it will add a nil value at the end.
117 118 119 |
# File 'lib/rubyhelper/array.rb', line 117 def mirror return self[0..(size/2+size%2-1)].zip(self[(size/2+size%2)..-1]) end |
#split! ⇒ Array
see #split
131 132 133 |
# File 'lib/rubyhelper/array.rb', line 131 def split! self.replace(self.strip) end |
#strip ⇒ Array
try to use the method split on every element of the array
124 125 126 |
# File 'lib/rubyhelper/array.rb', line 124 def strip self.do(:strip) end |
#sum(toadd = nil) ⇒ Integer, Array
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 If the argument toadd is no nill, then the self.size firsts elements will be added to self and returned examples: [1,2,3].sum() => [2,3,3] examples: [1,2].sum() => [2,3]
39 40 41 42 43 |
# File 'lib/rubyhelper/array.rb', line 39 def sum(toadd=nil) return (self.size > 0) ? (self.map{|e| e.to_s.to_i}.reduce(:+)) : (0) if toadd.nil? raise ArgumentError, 'Argument is not an Array' unless toadd.is_a? Array return self.zip(toadd).map{|pair| pair.map{|e| e.to_s.to_i}.reduce(&:+) } end |
#sumf(toadd = nil) ⇒ Float, Array
like #sum by with a to_f instead of to_i
49 50 51 52 53 |
# File 'lib/rubyhelper/array.rb', line 49 def sumf(toadd=nil) return (self.size > 0) ? (self.map{|e| e.to_s.to_f}.reduce(:+)) : (0.0) if toadd.nil? raise ArgumentError, 'Argument is not an Array' unless toadd.is_a? Array return self.zip(toadd).map{|pair| pair.map{|e| e.to_s.to_f}.reduce(&:+) } end |