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: b47e6add87f5
Choose a base ref
...
head repository: rubinius/rubinius
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 17172bfe24c4
Choose a head ref
  • 4 commits
  • 3 files changed
  • 2 contributors

Commits on Feb 12, 2015

  1. Trying to fix each_slice and each_with_index

    Tried to understand enum.c and produce something similar where a size
    function can be specific.
    bjfish committed Feb 12, 2015
    Copy the full SHA
    f418db7 View commit details

Commits on Feb 13, 2015

  1. Updating spec name and method name per code review

    Updating spec name and method name per code review
    bjfish committed Feb 13, 2015
    Copy the full SHA
    72a6d19 View commit details
  2. Copy the full SHA
    dd0341b View commit details
  3. Merge pull request #3315 from bjfish/enumerator_issues

    Trying to fix #3306 each_slice
    jemc committed Feb 13, 2015
    Copy the full SHA
    17172bf View commit details
Showing with 16 additions and 1 deletion.
  1. +5 −1 kernel/common/enumerable.rb
  2. +5 −0 kernel/common/enumerator.rb
  3. +6 −0 spec/ruby/core/enumerable/each_slice_spec.rb
6 changes: 5 additions & 1 deletion kernel/common/enumerable.rb
Original file line number Diff line number Diff line change
@@ -424,7 +424,11 @@ def each_cons(num)
end

def each_slice(slice_size)
return to_enum(:each_slice, slice_size) unless block_given?
unless block_given?
enum = to_enum(:each_slice, slice_size)
enum.size = Proc.new { enum.count }
return enum
end

n = Rubinius::Type.coerce_to_collection_index slice_size
raise ArgumentError, "invalid slice size: #{n}" if n <= 0
5 changes: 5 additions & 0 deletions kernel/common/enumerator.rb
Original file line number Diff line number Diff line change
@@ -35,6 +35,7 @@ def initialize(receiver_or_size=undefined, method_name=:each, *method_args, &blo
raise ArgumentError, "Enumerator#initialize requires a block when called without arguments"
end

size = receiver_or_size
receiver = receiver_or_size
end

@@ -151,6 +152,10 @@ def size
@size.kind_of?(Proc) ? @size.call : @size
end

def size=(size)
@size = size
end

def with_index(offset=0)
if offset
offset = Rubinius::Type.coerce_to offset, Integer, :to_int
6 changes: 6 additions & 0 deletions spec/ruby/core/enumerable/each_slice_spec.rb
Original file line number Diff line number Diff line change
@@ -56,4 +56,10 @@
multi = EnumerableSpecs::YieldsMulti.new
multi.each_slice(2).to_a.should == [[[1, 2], [3, 4, 5]], [[6, 7, 8, 9]]]
end

it "returns the correct size when no block is given" do
[1, 2, 3, 5].each_slice(2).size.should == 2
end


end