module MPatch::Include::Array
Public Instance Methods
return boolean by other array all element included or not in the target array
# File lib/mpatch/array.rb, line 72 def contain?(oth_array)#anothere array (oth_array & self) == oth_array end
return boolean by other array if any element included from the oth_array, return a true
# 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
return boolean if any element class is equal to th given class return a true , else false
# 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
generate params structure from array return_array
# 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
generate params structure from array *args + args_options {}
# File lib/mpatch/array.rb, line 172 def extract_options return self.dup.extract_class!(::Hash).reduce({},:merge!) end
generate params structure from array *args - args_options {}
# File lib/mpatch/array.rb, line 165 def extract_options! return self.extract_class!(::Hash).reduce({},:merge!) end
return index of the target element
# 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 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
-
}
#> {:hello=>123, :world=>123}
[:hello,“world”,:world,“hello”].map_hash{|k,v| { k => 123 } } #> {:hello=>123, :world=>123}
- [:hello,“world”],].map_hash{ |ary| { ary[0
-
> ary } }¶ ↑
#> {:hello=>“world”, :world=>“hello”}
# 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
generate params structure from array *args => [:opts,:args]
# File lib/mpatch/array.rb, line 123 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
remove n. element from the end and return a new object
# 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
remove n. element from the end and return the original object
# 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
do safe transpose
# 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
shifted first element and return new array
# File lib/mpatch/array.rb, line 56 def skip n=1 raise(ArgumentError) unless n.class <= Integer self[1 .. (n*(-1))] end
# 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
remove arguments or array of parameters from the main array
# 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