Module: ArrayHelper

Included in:
Array
Defined in:
lib/rubyhelper/array.rb

Instance Method Summary collapse

Instance Method Details

#averageInteger

Use the #sum and divide by the size of the array.

Returns:

  • (Integer)

    self.sum / self.size. 0 if no elements



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

#averagefFloat

Same than #average but use to_f instead of to_i

Returns:

  • (Float)

    self.sumf / self.size. 0.0 if no elements



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

#compactiArray

Same as #compact but remove empty string too see #compact

Returns:

  • (Array)

    compacted array



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

Returns:



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

Parameters:

  • method_to_exec (String or Symbol)

    method name

Returns:

Raises:

  • ArgumentError if argument is not a string/symbol



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

Returns:

Raises:

  • ArgumentError via #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”

Parameters:

  • sep (Array or String)

    array of separators or string as an uniq separator (join alias #join)

Returns:

Raises:

  • (ArgumentError)

    if sep in not an array or a string



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

Parameters:

  • n (Integer) (defaults to: 1)

    number of elements

Raises:

  • (ArgumentError)

    if n in not an integer

  • (ArgumentError)

    if n is lesser than 1



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

Parameters:

  • n (Integer) (defaults to: 1)

    number of elements

Raises:

  • (ArgumentError)

    if n in not an integer

  • (ArgumentError)

    if n is lesser than 1



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

#mirrorArray

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.

Returns:

  • (Array)

    mirrored array



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

Returns:



131
132
133
# File 'lib/rubyhelper/array.rb', line 131

def split!
  self.replace(self.strip)
end

#stripArray

try to use the method split on every element of the array

Returns:



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]

Parameters:

  • toadd (Array) (defaults to: nil)

    if not nil, the array will be added

Returns:

  • (Integer)

    the sum of each element (converted via .to_s.to_i)

  • (Array)

    an array wich contain the sum of a1 + a2

Raises:

  • (ArgumentError)


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

Returns:

  • (Float)

    the sum of each element (converted via .to_s.to_f)

  • (Array)

    an array wich contain the sum of a1 + a2

Raises:

  • (ArgumentError)


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