Sha256: 9bb3561854b58ee57204a74076de9fe93e187652c4ed1431b688cf1e59f8ecd2

Contents?: true

Size: 1.38 KB

Versions: 19

Compression:

Stored size: 1.38 KB

Contents

class Array

  # remove arguments or array of
  # parameters from the main array
  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

  # return index of the target element
  def index_of(target_element)
    array = self
    hash = Hash[array.map.with_index.to_a]
    return hash[target_element]
  end

  # remove n. element from the end
  # and return a new object
  def pinch n=1
    return self[0..(self.count-(n+1))]
  end

  # remove n. element from the end
  # and return the original object
  def pinch! n=1
    n.times do
      self.pop
    end
    return self
  end

  # return boolean by other array
  # all element included or
  # not in the target array
  def contain?(oth_array)#anothere array
    (oth_array & self) == oth_array
  end

  # do safe transpose
  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

  alias :contains? :contain?
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
procemon-0.4.2 lib/procemon/mpatch/array.rb
procemon-0.4.1 lib/procemon/mpatch/array.rb
procemon-0.4.0 lib/procemon/mpatch/array.rb
procemon-0.3.4 lib/procemon/mpatch/array.rb
procemon-0.3.3 lib/procemon/mpatch/array.rb
procemon-0.3.2 lib/procemon/mpatch/array.rb
procemon-0.3.1 lib/procemon/mpatch/array.rb
procemon-0.2.0 lib/procemon/mpatch/array.rb
procemon-0.1.5 lib/procemon/mpatch/array.rb
procemon-0.1.4 lib/procemon/mpatch/array.rb
procemon-0.1.3 lib/procemon/mpatch/array.rb
procemon-0.1.1 lib/procemon/mpatch/array.rb
procemon-0.0.9 lib/procemon/mpatch/array.rb
procemon-0.0.8 lib/procemon/mpatch/array.rb
procemon-0.0.7 lib/procemon/mpatch/array.rb
procemon-0.0.6 lib/procemon/mpatch/array.rb
procemon-0.0.5 lib/procemon/mpatch/array.rb
procemon-0.0.4 lib/procemon/mpatch/array.rb
procemon-0.0.3 lib/procemon/mpatch/array.rb