Class: Array
Instance Method Summary collapse
-
#contain?(oth_array) ⇒ Boolean
(also: #contains?)
return boolean by other array all element included or not in the target array.
-
#contain_any_of?(oth_array) ⇒ Boolean
(also: #contains_any_of?, #has_any_of?)
return boolean by other array if any element included from the oth_array, return a true.
-
#contain_element_of_class?(class_name) ⇒ Boolean
(also: #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.
-
#extract_class!(class_name) ⇒ Object
(also: #cut_class!)
generate params structure from array return_array.
-
#extract_options! ⇒ Object
(also: #extract_hash!)
generate params structure from array *args - options {}.
-
#index_of(target_element) ⇒ Object
return index of the target element.
-
#params_separation ⇒ Object
(also: #separate_params, #process_params)
generate params structure from array *args => [:opts,:args].
-
#pinch(n = 1) ⇒ Object
remove n.
-
#pinch!(n = 1) ⇒ Object
remove n.
-
#safe_transpose ⇒ Object
do safe transpose.
-
#trim(*args) ⇒ Object
remove arguments or array of parameters from the main array.
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
52 53 54 |
# File 'lib/mpatch/array.rb', line 52 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
59 60 61 62 63 64 65 66 |
# File 'lib/mpatch/array.rb', line 59 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
87 88 89 90 91 92 93 94 95 96 |
# File 'lib/mpatch/array.rb', line 87 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
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/mpatch/array.rb', line 123 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 ||= Array.new return return_value end |
#extract_options! ⇒ Object Also known as: extract_hash!
generate params structure from array *args - options {}
145 146 147 148 |
# File 'lib/mpatch/array.rb', line 145 def = self.extract_class! Hash return Hash[*] end |
#index_of(target_element) ⇒ Object
return index of the target element
28 29 30 31 32 |
# File 'lib/mpatch/array.rb', line 28 def index_of(target_element) array = self hash = Hash[array.map.with_index.to_a] return hash[target_element] end |
#params_separation ⇒ Object Also known as: separate_params, process_params
generate params structure from array *args => [:opts,:args]
103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/mpatch/array.rb', line 103 def params_separation = self.map { |element| if element.class == Hash element end }.uniq - [ nil ] #options.each{|e| self.delete(e) } arguments= self.dup - = Hash[*] return [,arguments] end |
#pinch(n = 1) ⇒ Object
remove n. element from the end and return a new object
36 37 38 |
# File 'lib/mpatch/array.rb', line 36 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
42 43 44 45 46 47 |
# File 'lib/mpatch/array.rb', line 42 def pinch! n=1 n.times do self.pop end return self end |
#safe_transpose ⇒ Object
do safe transpose
72 73 74 75 76 77 78 79 80 |
# File 'lib/mpatch/array.rb', line 72 def safe_transpose result = [] max_size = self.max { |a,b| a.size <=> b.size }.size max_size.times do |i| result[i] = Array.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
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/mpatch/array.rb', line 5 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= Array.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 |