Sha256: 5ff1e0b9122b67e1fd55860fc0cae8ae77bb8697961c3def8a0ccbade65709e7

Contents?: true

Size: 980 Bytes

Versions: 20

Compression:

Stored size: 980 Bytes

Contents

class Array

  # Delete multiple values from array.
  #
  #   a = [1,2,3,4]
  #   a.delete_values(1,2)   #=> [1,2]
  #   a                      #=> [3,4]
  #
  # CREDIT: Trans

  def delete_values(*values)
    d = []
    values.each{ |v| d << delete(v) }
    d
  end

  # Delete multiple values from array given
  # indexes or index range.
  #
  #   a = [1,2,3,4]
  #   a.delete_values_at(1,2)   #=> [2,3]
  #   a                         #=> [1,4]
  #   a = [1,2,3,4]
  #   a.delete_values_at(0..2)  #=> [1,2,3]
  #   a                         #=> [4]
  #
  # NOTE: It would be nice to see #delete_at incorporate this
  # funcitonaility.
  #
  # CREDIT: Trans

  def delete_values_at(*selectors)
    idx = []
    selectors.each{ |i|
      case i
      when Range
        idx.concat( i.to_a )
      else
        idx << i.to_i
      end
    }
    idx.uniq!
    dvals = values_at(*idx)
    idx = (0...size).to_a - idx
    self.replace( values_at(*idx) )
    return dvals
  end

end

Version data entries

20 entries across 19 versions & 2 rubygems

Version Path
facets-glimmer-3.2.0 lib/core/facets/array/delete_values.rb
facets-3.1.0 lib/core/facets/array/delete_values.rb
facets-3.0.0 lib/core/facets/array/delete_values.rb
facets-2.9.3 lib/core/facets/array/delete_values.rb
facets-2.9.2 lib/core/facets/array/delete_values.rb
facets-2.9.2 src/core/facets/array/delete_values.rb
facets-2.9.1 lib/core/facets/array/delete_values.rb
facets-2.9.0 lib/core/facets/array/delete_values.rb
facets-2.9.0.pre.2 lib/core/facets/array/delete_values.rb
facets-2.9.0.pre.1 lib/core/facets/array/delete_values.rb
facets-2.8.4 lib/core/facets/array/delete_values.rb
facets-2.8.3 lib/core/facets/array/delete_values.rb
facets-2.8.2 lib/core/facets/array/delete_values.rb
facets-2.8.1 lib/core/facets/array/delete_values.rb
facets-2.8.0 lib/core/facets/array/delete_values.rb
facets-2.7.0 lib/core/facets/array/delete_values.rb
facets-2.6.0 lib/core/facets/array/delete_values.rb
facets-2.5.0 lib/core/facets/array/delete_values.rb
facets-2.5.1 lib/core/facets/array/delete_values.rb
facets-2.5.2 lib/core/facets/array/delete_values.rb