Skip to content

Commit

Permalink
Showing 4 changed files with 34 additions and 3 deletions.
13 changes: 13 additions & 0 deletions core/src/main/java/org/jruby/javasupport/ext/JavaLang.java
Original file line number Diff line number Diff line change
@@ -117,6 +117,19 @@ public static IRubyObject each_with_index(final ThreadContext context, final IRu
return context.nil;
}

@JRubyMethod(name = { "to_a", "entries" })
public static IRubyObject to_a(final ThreadContext context, final IRubyObject self, final Block block) {
final Ruby runtime = context.runtime;
final RubyArray ary = runtime.newArray();
java.lang.Iterable iterable = unwrapJavaObject(self);
java.util.Iterator iterator = iterable.iterator();
while ( iterator.hasNext() ) {
final Object value = iterator.next();
ary.append( convertJavaToUsableRubyObject(runtime, value) );
}
return ary;
}

}

@JRubyClass(name = "Java::JavaLang::Comparable", include = "Comparable")
6 changes: 3 additions & 3 deletions core/src/main/java/org/jruby/javasupport/ext/JavaUtil.java
Original file line number Diff line number Diff line change
@@ -137,7 +137,7 @@ public static IRubyObject each_with_index(final ThreadContext context, final IRu
return JavaLang.Iterable.each_with_index(context, self, block);
}

@JRubyMethod(name = { "include?", "member?" })
@JRubyMethod(name = { "include?", "member?" }) // @override Enumerable#include?
public static RubyBoolean include_p(final ThreadContext context, final IRubyObject self, final IRubyObject obj) {
final java.util.Collection coll = unwrapJavaObject(self);
return context.runtime.newBoolean( coll.contains( obj.toJava(java.lang.Object.class) ) );
@@ -171,13 +171,13 @@ public static IRubyObject append(final IRubyObject self, final IRubyObject item)
return self;
}

@JRubyMethod
@JRubyMethod(name = { "to_a", "entries" })
public static RubyArray to_a(final ThreadContext context, final IRubyObject self) {
final Object[] array = ((java.util.Collection) unwrapJavaObject(self)).toArray();
if ( IRubyObject.class.isAssignableFrom(array.getClass().getComponentType()) ) {
return RubyArray.newArrayMayCopy(context.runtime, (IRubyObject[]) array);
}
return RubyArray.newArrayMayCopy(context.runtime, convertJavaArrayToRuby(context.runtime, array));
return RubyArray.newArrayNoCopy(context.runtime, convertJavaArrayToRuby(context.runtime, array));
}

@JRubyMethod(name = "+", required = 1)
3 changes: 3 additions & 0 deletions spec/java_integration/extensions/collection_spec.rb
Original file line number Diff line number Diff line change
@@ -79,6 +79,9 @@
coll = java.util.ArrayDeque.new(@data)
expect(coll.to_a).to eq(@data.to_a)

coll = java.util.LinkedHashSet.new(@data)
expect(coll.entries).to eq(@data.to_a)

coll = java.util.HashSet.new
expect(coll.to_a).to eq([])
end
15 changes: 15 additions & 0 deletions spec/java_integration/extensions/iterable_spec.rb
Original file line number Diff line number Diff line change
@@ -47,4 +47,19 @@
expect( paths.last ).to eq 'ITERABLE_SPEC.RB'
end

it 'converts #to_a' do
file_system = java.nio.file.FileSystems.getDefault
path = file_system.getPath(__FILE__)
expect( path.to_a ).to_not be_empty
expect( path.to_a ).to eql iterate_path(path)
end

private

def iterate_path(path)
res = [] ; it = path.iterator
while it.hasNext ; res << it.next end
res
end

end

0 comments on commit c74057f

Please sign in to comment.