Sha256: 07274f0bdabefc21f9192cd98ed08a2de1edacc3f85c3732949d2c4db59711b8

Contents?: true

Size: 1.04 KB

Versions: 9

Compression:

Stored size: 1.04 KB

Contents

class Array

  # 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]
  #

  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


#  _____         _
# |_   _|__  ___| |_
#   | |/ _ \/ __| __|
#   | |  __/\__ \ |_
#   |_|\___||___/\__|
#
=begin test

  require 'test/unit'

  class TCArray < Test::Unit::TestCase

    def test_delete_values_at
      a = [1,2,3,4]
      assert_equal( [2,3], a.delete_values_at(1,2) )
      assert_equal( [1,4], a )
      a = [1,2,3,4]
      assert_equal( [1,2,3], a.delete_values_at(0..2) )
      assert_equal( [4], a )
    end

  end

=end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
facets-1.7.38 lib/facets/core/array/delete_values_at.rb
facets-1.7.30 lib/facets/core/array/delete_values_at.rb
facets-1.7.46 lib/facets/core/array/delete_values_at.rb
facets-1.8.51 lib/facets/core/array/delete_values_at.rb
facets-1.8.0 lib/facets/core/array/delete_values_at.rb
facets-1.8.20 lib/facets/core/array/delete_values_at.rb
facets-1.8.49 lib/facets/core/array/delete_values_at.rb
facets-1.8.54 lib/facets/core/array/delete_values_at.rb
facets-1.8.8 lib/facets/core/array/delete_values_at.rb