Module: MPatch::Include::Array
- Defined in:
- lib/mpatch/array.rb
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 + args_options {}.
-
#extract_options! ⇒ Object
(also: #extract_hash!)
generate params structure from array *args - args_options {}.
-
#index_of(target_element) ⇒ Object
return index of the target element.
-
#map_hash(*args, &block) ⇒ Object
(also: #map2hash)
map hash will work just alike map but instead of an array it will return a hash obj.
-
#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.
-
#skip(n = 1) ⇒ Object
shifted first element and return new array.
- #skip!(n = 1) ⇒ Object
-
#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
72 73 74 |
# File 'lib/mpatch/array.rb', line 72 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
79 80 81 82 83 84 85 86 |
# File 'lib/mpatch/array.rb', line 79 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
107 108 109 110 111 112 113 114 115 116 |
# File 'lib/mpatch/array.rb', line 107 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
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/mpatch/array.rb', line 143 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 + args_options {}
172 173 174 |
# File 'lib/mpatch/array.rb', line 172 def return self.dup.extract_class!(::Hash).reduce({},:merge!) end |
#extract_options! ⇒ Object Also known as: extract_hash!
generate params structure from array *args - args_options {}
165 166 167 |
# File 'lib/mpatch/array.rb', line 165 def return self.extract_class!(::Hash).reduce({},:merge!) 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 |
#map_hash(*args, &block) ⇒ Object Also known as: map2hash
map hash will work just alike map but instead of an array it will return a hash obj
- :hello, “world”,:world , “hello”].map_hash{|k,v| [ k , 123
-
}
#> :world=>123
[:hello,“world”,:world,“hello”].map_hash{|k,v| { k => 123 } } #> :world=>123
- [:hello,“world”],].map_hash{ |ary| { ary[0
-
> ary } }
#> :world=>“hello”
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 |
# File 'lib/mpatch/array.rb', line 188 def map_hash *args,&block tmp_hash= ::Hash.new(*args) self.map(&block).each do |hash| case true when hash.class <= ::Array tmp_hash.deep_merge!(::Hash[*hash]) when hash.class <= ::Hash tmp_hash.deep_merge!(hash) else raise ArgumentError, "invalid input as last valie for #{__method__}: #{hash}/#{hash.class}" end end return tmp_hash end |
#params_separation ⇒ Object Also known as: separate_params, process_params
generate params structure from array *args => [:opts,:args]
123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/mpatch/array.rb', line 123 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
40 41 42 43 |
# File 'lib/mpatch/array.rb', line 40 def pinch n=1 raise(ArgumentError) unless n.class <= Integer return self[0..(self.count-(n+1))] end |
#pinch!(n = 1) ⇒ Object
remove n. element from the end and return the original object
47 48 49 50 51 52 53 |
# File 'lib/mpatch/array.rb', line 47 def pinch! n=1 raise(ArgumentError) unless n.class <= Integer n.times do self.pop end return self end |
#safe_transpose ⇒ Object
do safe transpose
92 93 94 95 96 97 98 99 100 |
# File 'lib/mpatch/array.rb', line 92 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 |
#skip(n = 1) ⇒ Object
shifted first element and return new array
56 57 58 59 |
# File 'lib/mpatch/array.rb', line 56 def skip n=1 raise(ArgumentError) unless n.class <= Integer self[1 .. (n*(-1))] end |
#skip!(n = 1) ⇒ Object
61 62 63 64 65 66 67 |
# File 'lib/mpatch/array.rb', line 61 def skip! n=1 raise(ArgumentError) unless n.class <= Integer n.times do self.shift end return self 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 += 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 |