Module: MPatch::Include::Array

Defined in:
lib/mpatch/array.rb

Instance Method Summary collapse

Instance Method Details

#contain?(oth_array) ⇒ Boolean Also known as: contains?

return boolean by other array all element included or not in the target array

Returns:

  • (Boolean)


56
57
58
# File 'lib/mpatch/array.rb', line 56

def contain?(oth_array)#anothere array
  (oth_array & self) == oth_array
end

#contain_any_of?(oth_array) ⇒ Boolean Also known as: contains_any_of?, has_any_of?

return boolean by other array if any element included from the oth_array, return a true

Returns:

  • (Boolean)


63
64
65
66
67
68
69
70
# File 'lib/mpatch/array.rb', line 63

def contain_any_of?(oth_array)
  oth_array.each do |element|
    if self.include? element
      return true
    end
  end
  return false
end

#contain_element_of_class?(class_name) ⇒ Boolean Also known as: contains_element_of_class?, has_element_of_class?

return boolean if any element class is equal to th given class return a true , else false

Returns:

  • (Boolean)


91
92
93
94
95
96
97
98
99
100
# File 'lib/mpatch/array.rb', line 91

def contain_element_of_class?(class_name)
  target_array= self.map{|e| e.class }.uniq
  if class_name.class != ::Class
    raise ::ArgumentError, "Argument must be a Class!"
  end
  if target_array.include? class_name
    return true
  end
  return false
end

#extract_class!(class_name) ⇒ Object Also known as: cut_class!

generate params structure from array return_array



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/mpatch/array.rb', line 127

def extract_class! class_name

  unless class_name.class <= ::Class
    raise ::ArgumentError, "parameter must be a class name"
  end

  return_value= self.map { |element|
    if element.class <= class_name
      element
    end
  }.uniq - [ nil ]
  return_value.each{|e| self.delete(e) }

  return_value ||= self.class.new

  return return_value

end

#extract_options!Object Also known as: extract_hash!

generate params structure from array *args - options {}



149
150
151
152
# File 'lib/mpatch/array.rb', line 149

def extract_options!
  options= self.extract_class! ::Hash
  return ::Hash[*options]
end

#index_of(target_element) ⇒ Object

return index of the target element



32
33
34
35
36
# File 'lib/mpatch/array.rb', line 32

def index_of(target_element)
  array = self
  hash = ::Hash[array.map.with_index.to_a]
  return hash[target_element]
end

#params_separationObject Also known as: separate_params, process_params

generate params structure from array *args => [:opts,:args]



107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/mpatch/array.rb', line 107

def params_separation

  options= self.map { |element|
    if element.class == ::Hash
      element
    end
  }.uniq - [ nil ]
  #options.each{|e| self.delete(e) }
  arguments= self.dup - options
  options= ::Hash[*options]

  return [options,arguments]

end

#pinch(n = 1) ⇒ Object

remove n. element from the end and return a new object



40
41
42
# File 'lib/mpatch/array.rb', line 40

def pinch n=1
  return self[0..(self.count-(n+1))]
end

#pinch!(n = 1) ⇒ Object

remove n. element from the end and return the original object



46
47
48
49
50
51
# File 'lib/mpatch/array.rb', line 46

def pinch! n=1
  n.times do
    self.pop
  end
  return self
end

#safe_transposeObject

do safe transpose



76
77
78
79
80
81
82
83
84
# File 'lib/mpatch/array.rb', line 76

def safe_transpose
  result = []
  max_size = self.max { |a,b| a.size <=> b.size }.size
  max_size.times do |i|
    result[i] = self.class.new(self.first.size)
    self.each_with_index { |r,j| result[i][j] = r[i] }
  end
  result
end

#trim(*args) ⇒ Object

remove arguments or array of parameters from the main array



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/mpatch/array.rb', line 9

def trim(*args)

  args.dup.each do |one_element|
    if one_element.class <= ::Array
      args.delete_at(args.index(one_element))
      args= args+one_element
    end
  end

  delete_array= self.class.new
  args.each do |one_element|
    index= self.index(one_element)
    unless index.nil?
      delete_array.push index
      self.delete_at(index)
    end
  end

  return self

end