module MPatch::Include::Array

Public Instance Methods

contain?(oth_array) click to toggle source

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
Also aliased as: contains?
contain_any_of?(oth_array) click to toggle source

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
Also aliased as: contains_any_of?, has_any_of?
contain_element_of_class?(class_name) click to toggle source

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
contains?(oth_array)
Alias for: contain?
contains_any_of?(oth_array)
Alias for: contain_any_of?
contains_element_of_class?(class_name)
cut_class!(class_name)
Alias for: extract_class!
extract_class!(class_name) click to toggle source

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
Also aliased as: cut_class!
extract_hash()
Alias for: extract_options
extract_hash!()
Alias for: extract_options!
extract_options() click to toggle source

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
Also aliased as: extract_hash
extract_options!() click to toggle source

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
Also aliased as: extract_hash!
has_any_of?(oth_array)
Alias for: contain_any_of?
has_element_of_class?(class_name)
index_of(target_element) click to toggle source

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
map2hash(*args,&block)
Alias for: map_hash
map_hash(*args,&block) click to toggle source

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
Also aliased as: map2hash
params_separation() click to toggle source

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
Also aliased as: separate_params, process_params
pinch(n=1) click to toggle source

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
pinch!(n=1) click to toggle source

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
process_params()
Alias for: params_separation
safe_transpose() click to toggle source

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
separate_params()
Alias for: params_separation
skip(n=1) click to toggle source

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
skip!(n=1) click to toggle source
# 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) click to toggle source

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