Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: rubinius/rubinius
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 419ee42fd897
Choose a base ref
...
head repository: rubinius/rubinius
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 33e10351a106
Choose a head ref
  • 3 commits
  • 4 files changed
  • 1 contributor

Commits on Jan 26, 2015

  1. Added spec for Enumerable#enum_with_index

    It behaves like (and should be aliased to) Enumerable#each_with_index,
    so this commit also moved the current Enumerable#each_with_index
    specs into Enumerable's shared directory.
    sshao committed Jan 26, 2015
    Copy the full SHA
    5924993 View commit details
  2. removed Enumerable#each_with_index specs' ruby_version_gaurds

    removes the 1.8.7 ruby_version_guard, and deletes the spec
    guarded by ruby_version_is 1.9
    sshao committed Jan 26, 2015
    Copy the full SHA
    eb86227 View commit details
  3. Added Enumerable#enum_with_index back

    looks like it was deleted by the most recent merge from master
    sshao committed Jan 26, 2015
    Copy the full SHA
    33e1035 View commit details
2 changes: 2 additions & 0 deletions kernel/common/enumerable.rb
Original file line number Diff line number Diff line change
@@ -114,6 +114,8 @@ def each_with_index(*args)
self
end

alias_method :enum_with_index, :each_with_index

def grep(pattern)
ary = []

52 changes: 2 additions & 50 deletions spec/ruby/core/enumerable/each_with_index_spec.rb
Original file line number Diff line number Diff line change
@@ -1,55 +1,7 @@
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../fixtures/classes', __FILE__)
require File.expand_path('../shared/each_with_index', __FILE__)

describe "Enumerable#each_with_index" do

before :each do
@b = EnumerableSpecs::Numerous.new(2, 5, 3, 6, 1, 4)
end

it "passes each element and its index to block" do
@a = []
@b.each_with_index { |o, i| @a << [o, i] }
@a.should == [[2, 0], [5, 1], [3, 2], [6, 3], [1, 4], [4, 5]]
end

it "provides each element to the block" do
acc = []
obj = EnumerableSpecs::EachDefiner.new()
res = obj.each_with_index {|a,i| acc << [a,i]}
acc.should == []
obj.should == res
end

it "provides each element to the block and its index" do
acc = []
res = @b.each_with_index {|a,i| acc << [a,i]}
[[2, 0], [5, 1], [3, 2], [6, 3], [1, 4], [4, 5]].should == acc
res.should eql(@b)
end

it "binds splat arguments properly" do
acc = []
res = @b.each_with_index { |*b| c,d = b; acc << c; acc << d }
[2, 0, 5, 1, 3, 2, 6, 3, 1, 4, 4, 5].should == acc
res.should eql(@b)
end

ruby_version_is '1.8.7' do
it "returns an enumerator if no block" do
e = @b.each_with_index
e.should be_an_instance_of(enumerator_class)
e.to_a.should == [[2, 0], [5, 1], [3, 2], [6, 3], [1, 4], [4, 5]]
end
end

ruby_version_is '1.9' do
it "passes extra parameters to each" do
count = EnumerableSpecs::EachCounter.new(:apple)
e = count.each_with_index(:foo, :bar)
e.to_a.should == [[:apple, 0]]
count.arguments_passed.should == [:foo, :bar]
end

end
it_behaves_like(:each_with_index, :each_with_index)
end
6 changes: 6 additions & 0 deletions spec/ruby/core/enumerable/enum_with_index_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
require File.expand_path('../shared/each_with_index', __FILE__)

describe "Enumerable#each_with_index" do
it_behaves_like(:each_with_index, :enum_with_index)
end

39 changes: 39 additions & 0 deletions spec/ruby/core/enumerable/shared/each_with_index.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
describe :each_with_index, :shared => true do
before :each do
@b = EnumerableSpecs::Numerous.new(2, 5, 3, 6, 1, 4)
end

it "passes each element and its index to block" do
@a = []
@b.send(@method) { |o, i| @a << [o, i] }
@a.should == [[2, 0], [5, 1], [3, 2], [6, 3], [1, 4], [4, 5]]
end

it "provides each element to the block" do
acc = []
obj = EnumerableSpecs::EachDefiner.new()
res = obj.send(@method) {|a,i| acc << [a,i]}
acc.should == []
obj.should == res
end

it "provides each element to the block and its index" do
acc = []
res = @b.send(@method) {|a,i| acc << [a,i]}
[[2, 0], [5, 1], [3, 2], [6, 3], [1, 4], [4, 5]].should == acc
res.should eql(@b)
end

it "binds splat arguments properly" do
acc = []
res = @b.send(@method) { |*b| c,d = b; acc << c; acc << d }
[2, 0, 5, 1, 3, 2, 6, 3, 1, 4, 4, 5].should == acc
res.should eql(@b)
end

it "returns an enumerator if no block" do
e = @b.send(@method)
e.should be_an_instance_of(enumerator_class)
e.to_a.should == [[2, 0], [5, 1], [3, 2], [6, 3], [1, 4], [4, 5]]
end
end