Sha256: cfab708b22776db59a91b9859f07d089b1c114b931daee65983c2ce6ce196750

Contents?: true

Size: 1.29 KB

Versions: 83

Compression:

Stored size: 1.29 KB

Contents

require File.dirname(File.join(__rhoGetCurrentDir(), __FILE__)) + '/../../spec_helper'
require File.dirname(File.join(__rhoGetCurrentDir(), __FILE__)) + '/fixtures/classes'

describe "Array#each_index" do
  it "passes the index of each element to the block" do
    a = []
    x = ['a', 'b', 'c', 'd']
    x.each_index { |i| a << i }.should equal(x)
    a.should == [0, 1, 2, 3]
  end

  it "passes the index of each element to the block even if the array is changed during iteration" do
    a = []
    x = [10, 11, 12, 13,]
    x.each_index {|i| a << i; x << x[i]+5 if (x[i]%2).zero? }.should equal(x)
    a.should == [0, 1, 2, 3, 4, 5]
  end

  it "passes the index from 0..size even if size changes" do
    a = []
    x = [10, 11, 12, 13, 14]
    x.each_index {|i| a << i; x.pop if (x[i]%2).zero? }.should equal(x)
    a.should == [0, 1, 2]

    a = []
    x = [10, 11, 12, 13, 14]
    x.each_index {|i| a << i; x.shift if (x[i]%2).zero? }.should equal(x)
    a.should == [0, 1, 2]
  end

  ruby_version_is '' ... '1.8.7' do
    it 'raises a LocalJumpError if no block given' do
      lambda{ [1,2].each_index }.should raise_error(LocalJumpError)
    end
  end
  ruby_version_is '1.8.7' do
    it 'returns an Enumerator if no block given' do
      [1,2].each_index.should be_kind_of(enumerator_class)
    end
  end
end

Version data entries

83 entries across 83 versions & 1 rubygems

Version Path
rhodes-3.1.1 spec/framework_spec/app/spec/core/array/each_index_spec.rb
rhodes-3.1.1.beta spec/framework_spec/app/spec/core/array/each_index_spec.rb
rhodes-3.1.0 spec/framework_spec/app/spec/core/array/each_index_spec.rb
rhodes-3.1.0.beta.5 spec/framework_spec/app/spec/core/array/each_index_spec.rb
rhodes-3.1.0.beta.4 spec/framework_spec/app/spec/core/array/each_index_spec.rb
rhodes-3.1.0.beta.3 spec/framework_spec/app/spec/core/array/each_index_spec.rb
rhodes-3.1.0.beta.2 spec/framework_spec/app/spec/core/array/each_index_spec.rb
rhodes-3.1.0.beta.1 spec/framework_spec/app/spec/core/array/each_index_spec.rb
rhodes-3.0.2 spec/framework_spec/app/spec/core/array/each_index_spec.rb
rhodes-3.0.2.beta.1 spec/framework_spec/app/spec/core/array/each_index_spec.rb
rhodes-3.0.1 spec/framework_spec/app/spec/core/array/each_index_spec.rb
rhodes-3.0.1.beta.8 spec/framework_spec/app/spec/core/array/each_index_spec.rb
rhodes-3.0.1.beta.7 spec/framework_spec/app/spec/core/array/each_index_spec.rb
rhodes-3.0.1.beta.6 spec/framework_spec/app/spec/core/array/each_index_spec.rb
rhodes-3.0.1.beta.5 spec/framework_spec/app/spec/core/array/each_index_spec.rb
rhodes-3.0.1.beta.4 spec/framework_spec/app/spec/core/array/each_index_spec.rb
rhodes-3.0.1.beta.3 spec/framework_spec/app/spec/core/array/each_index_spec.rb
rhodes-3.0.1.beta.2 spec/framework_spec/app/spec/core/array/each_index_spec.rb
rhodes-3.0.0 spec/framework_spec/app/spec/core/array/each_index_spec.rb
rhodes-3.0.0.beta.7 spec/framework_spec/app/spec/core/array/each_index_spec.rb