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: jruby/jruby
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 05e6e7e1090d
Choose a base ref
...
head repository: jruby/jruby
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: d79adf43e95a
Choose a head ref
  • 2 commits
  • 2 files changed
  • 2 contributors

Commits on Dec 13, 2015

  1. add Enumerable#chunk_while

    add chunk_while_contiguously_increasing_integers test to test_enum.rb
    cthulhua committed Dec 13, 2015
    Copy the full SHA
    aa399d4 View commit details
  2. Merge pull request #3517 from cthulhua/feature-enumerable-chunk_while

    [ruby-2.3 feature #10769] add Enumerable#chunk_while, spec for it
    kares committed Dec 13, 2015
    Copy the full SHA
    d79adf4 View commit details
Showing with 34 additions and 0 deletions.
  1. +29 −0 core/src/main/ruby/jruby/kernel/enumerable.rb
  2. +5 −0 test/mri/ruby/test_enum.rb
29 changes: 29 additions & 0 deletions core/src/main/ruby/jruby/kernel/enumerable.rb
Original file line number Diff line number Diff line change
@@ -91,6 +91,35 @@ def slice_when(&block)
end
end
end

def chunk_while(&block)
raise ArgumentError.new("missing block") unless block
return Enumerator.new {|e| } if empty?
return Enumerator.new {|e| e.yield self } if length < 2

Enumerator.new do |enum|
ary = nil
last_after = nil
each_cons(2) do |before, after|
last_after = after
match = block.call before, after

ary ||= []
if match
ary << before
else
ary << before
enum.yield ary
ary = []
end
end

unless ary.nil?
ary << last_after
enum.yield ary
end
end
end

def lazy
klass = Enumerator::Lazy::LAZY_WITH_NO_BLOCK # Note: class_variable_get is private in 1.8
5 changes: 5 additions & 0 deletions test/mri/ruby/test_enum.rb
Original file line number Diff line number Diff line change
@@ -598,6 +598,11 @@ def test_slice_when_contiguously_increasing_integers
assert_equal([[1], [4], [9,10,11,12], [15,16], [19,20,21]], e.to_a)
end

def test_chunk_while_contiguously_increasing_integers
e = [1,4,9,10,11,12,15,16,19,20,21].chunk_while {|i, j| i+1 == j }
assert_equal([[1], [4], [9,10,11,12], [15,16], [19,20,21]], e.to_a)
end

def test_detect
@obj = ('a'..'z')
assert_equal('c', @obj.detect {|x| x == 'c' })