Skip to content
This repository
tag: v0.7.5
Fetching contributors…

Cannot retrieve contributors at this time

file 38 lines (30 sloc) 0.905 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
require File.dirname(__FILE__) + '/../../spec_helper'
require File.dirname(__FILE__) + '/fixtures/classes'

describe "Array#reverse_each" do
  it "traverses array in reverse order and pass each element to block" do
    a = []
    [1, 3, 4, 6].reverse_each { |i| a << i }
    a.should == [6, 4, 3, 1]
  end

  it "properly handles recursive arrays" do
    res = []
    empty = ArraySpecs.empty_recursive_array
    empty.reverse_each { |i| res << i }
    res.should == [empty]

    res = []
    array = ArraySpecs.recursive_array
    array.reverse_each { |i| res << i }
    res.should == [array, array, array, array, array, 3.0, 'two', 1]
  end

  it "does not fail when removing elements from block" do
    ary = [0, 0, 1, 1, 3, 2, 1, :x]

    count = 0

    ary.reverse_each do |item|
      count += 1

      if item == :x then
        ary.slice!(1..-1)
      end
    end

    count.should == 2
  end
end
Something went wrong with that request. Please try again.