HEX
Server: Apache
System: Linux s198.coreserver.jp 5.15.0-151-generic #161-Ubuntu SMP Tue Jul 22 14:25:40 UTC 2025 x86_64
User: nagasaki (10062)
PHP: 7.1.33
Disabled: NONE
Upload Files
File: //usr/local/rvm/src/ruby-2.7.4/spec/ruby/core/array/delete_at_spec.rb
require_relative '../../spec_helper'
require_relative 'fixtures/classes'

describe "Array#delete_at" do
  it "removes the element at the specified index" do
    a = [1, 2, 3, 4]
    a.delete_at(2)
    a.should == [1, 2, 4]
    a.delete_at(-1)
    a.should == [1, 2]
  end

  it "returns the removed element at the specified index" do
    a = [1, 2, 3, 4]
    a.delete_at(2).should == 3
    a.delete_at(-1).should == 4
  end

  it "returns nil and makes no modification if the index is out of range" do
    a = [1, 2]
    a.delete_at(3).should == nil
    a.should == [1, 2]
    a.delete_at(-3).should == nil
    a.should == [1, 2]
  end

  it "tries to convert the passed argument to an Integer using #to_int" do
    obj = mock('to_int')
    obj.should_receive(:to_int).and_return(-1)
    [1, 2].delete_at(obj).should == 2
  end

  it "accepts negative indices" do
    a = [1, 2]
    a.delete_at(-2).should == 1
  end

  it "raises a #{frozen_error_class} on a frozen array" do
    -> { [1,2,3].freeze.delete_at(0) }.should raise_error(frozen_error_class)
  end

  ruby_version_is ''...'2.7' do
    it "keeps tainted status" do
      ary = [1, 2]
      ary.taint
      ary.tainted?.should be_true
      ary.delete_at(0)
      ary.tainted?.should be_true
      ary.delete_at(0) # now empty
      ary.tainted?.should be_true
    end

    it "keeps untrusted status" do
      ary = [1, 2]
      ary.untrust
      ary.untrusted?.should be_true
      ary.delete_at(0)
      ary.untrusted?.should be_true
      ary.delete_at(0) # now empty
      ary.untrusted?.should be_true
    end
  end
end