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: 925310c49a03
Choose a base ref
...
head repository: jruby/jruby
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: b104023283c6
Choose a head ref
  • 2 commits
  • 5 files changed
  • 1 contributor

Commits on Aug 1, 2016

  1. [ji] align return values for java.lang.Iterable (and thus all Collect…

    …ion) ...
    
    from each and each_with_index to be consistent with Ruby (return self)
    kares committed Aug 1, 2016
    Copy the full SHA
    9020527 View commit details
  2. Copy the full SHA
    b104023 View commit details
4 changes: 2 additions & 2 deletions core/src/main/java/org/jruby/javasupport/ext/JavaLang.java
Original file line number Diff line number Diff line change
@@ -92,7 +92,7 @@ public static IRubyObject each(final ThreadContext context, final IRubyObject se
final Object value = iterator.next();
block.yield(context, convertJavaToUsableRubyObject(runtime, value));
}
return context.nil;
return self;
}

@JRubyMethod
@@ -114,7 +114,7 @@ public static IRubyObject each_with_index(final ThreadContext context, final IRu
block.yield(context, RubyArray.newArray(runtime, rValue, index));
}
}
return context.nil;
return self;
}

@JRubyMethod(name = { "to_a", "entries" }) // @override Enumerable#to_a
17 changes: 17 additions & 0 deletions core/src/main/ruby/jruby/java/java_ext/java.lang.rb
Original file line number Diff line number Diff line change
@@ -45,6 +45,7 @@ module Java::java::lang::Iterable
include ::Enumerable

# Ruby style `Enumerable#each` iteration for Java iterable types.
# @return [Java::java::util::Iterable] self (since 9.1.3)
# @return [Enumerator] if called without a block to yield to
def each(&block)
# stub implemented in org.jruby.javasupport.ext.JavaLang.java
@@ -53,6 +54,7 @@ def each(&block)
end

# Ruby style `Enumerable#each_with_index` for Java iterable types.
# @return [Java::java::util::Iterable] self (since 9.1.3)
# @return [Enumerator] if called without a block to yield to
def each_with_index(&block)
# stub implemented in org.jruby.javasupport.ext.JavaLang.java
@@ -63,6 +65,21 @@ def each_with_index(&block)
# index += 1
# end
end

# Re-defined `Enumerable#to_a`.
# @return [Array]
# @since 9.1.3
def to_a
# stub implemented in org.jruby.javasupport.ext.JavaLang.java
end
alias entries to_a

# Re-defined `Enumerable#count`.
# @return [Integer] matched elements count
# @since 9.1.3
def count(obj = nil, &block)
# stub implemented in org.jruby.javasupport.ext.JavaLang.java
end
end if false

# *java.lang.Comparable* mixes in Ruby's `Comparable` support.
4 changes: 3 additions & 1 deletion core/src/main/ruby/jruby/java/java_ext/java.util.rb
Original file line number Diff line number Diff line change
@@ -20,7 +20,7 @@ def each_with_index(&block)
end

# Re-implemented for efficiency, so that we do not (`#each`) loop over the collection
# for types where its not necessary (e.g. *java.util.Set* instances), using (native) `contains`.
# for types where its not necessary (e.g. *java.util.Set* instances), uses (native) `java.util.Collection#contains`.
# @see Java::java::lang::Iterable#include?
# @return [true, false]
# @since 9.1.3
@@ -92,6 +92,8 @@ def join(*args)
def to_a
# stub implemented in org.jruby.javasupport.ext.JavaUtil.java
end
# @since 9.1.3
alias entries to_a

# Return a dup-ed collection (if possible).
# @example
6 changes: 4 additions & 2 deletions spec/java_integration/extensions/collection_spec.rb
Original file line number Diff line number Diff line change
@@ -13,8 +13,9 @@
expect(data).to eq @data

data = []
java.util.concurrent.LinkedBlockingQueue.new(@data).each { |elem| data << elem }
ret = java.util.concurrent.LinkedBlockingQueue.new(@data).each { |elem| data << elem }
expect(data).to eq @data
expect(ret).to be_a java.util.concurrent.LinkedBlockingQueue
end

it 'iterates with an Enumerator on #each' do
@@ -30,9 +31,10 @@
set = java.util.LinkedHashSet.new
@data.each { |elem| set.add elem }
data = []; idx = []
set.each_with_index { |elem, i| data << elem; idx << i }
ret = set.each_with_index { |elem, i| data << elem; idx << i }
expect(data).to eq @data
expect(idx).to eq [0, 1, 2, 3]
expect(ret).to be set

data = []
java.util.concurrent.LinkedBlockingQueue.new.each_with_index { |elem| data << elem }
15 changes: 12 additions & 3 deletions spec/java_integration/extensions/iterable_spec.rb
Original file line number Diff line number Diff line change
@@ -8,9 +8,10 @@
file_system = java.nio.file.FileSystems.getDefault
path = file_system.getPath(__FILE__)
paths = []
path.each { |p| paths << p.to_s }
ret = path.each { |p| paths << p.to_s }
expect( paths ).to_not be_empty
expect( paths.last ).to eq 'iterable_spec.rb'
expect( ret ).to eq path
end

it 'iterates with an Enumerator on #each' do
@@ -23,11 +24,19 @@
it 'iterates using #each_with_index' do
file_system = java.nio.file.FileSystems.getDefault
path = file_system.getPath(__FILE__)
paths = []; idxs = []
path.each_with_index { |p| paths << p }
paths = []
ret = path.each_with_index { |p| paths << p }
expect( paths ).to_not be_empty
expect( paths[-1][0].to_s ).to eq 'iterable_spec.rb'
expect( paths[-1][1] ).to eq paths.size - 1
expect( ret ).to eq path

paths = []; idxs = []
ret = path.each_with_index { |p, i| paths << p; idxs << i }
expect( paths ).to_not be_empty
expect( paths[-1].to_s ).to eq 'iterable_spec.rb'
expect( idxs[0] ).to eq 0
expect( idxs[-1] ).to eq paths.size - 1
end

it 'iterates with an Enumerator on #each_with_index' do