Skip to content

Commit 8cb2b3c

Browse files
committed
[Truffle] Refactoring Array#zip.
1 parent eb8f62e commit 8cb2b3c

File tree

2 files changed

+10
-15
lines changed

2 files changed

+10
-15
lines changed

truffle/src/main/java/org/jruby/truffle/nodes/core/ArrayNodes.java

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4587,29 +4587,25 @@ public RubyArray zipObjectObject(RubyArray array, Object[] others) {
45874587

45884588
@Specialization(guards = {"!isOtherSingleObjectArray"})
45894589
public Object zipObjectObjectNotSingleObject(VirtualFrame frame, RubyArray array, Object[] others) {
4590-
RubyBasicObject proc = RubyArguments.getBlock(frame.getArguments());
4591-
if (proc == null) {
4592-
proc = nil();
4593-
}
4594-
return ruby(frame, "zip_internal(nil, *others)", "others", new RubyArray(getContext().getCoreLibrary().getArrayClass(), others, others.length), "block", proc);
4590+
return zipRuby(frame, others);
45954591
}
45964592

45974593
@Specialization(guards = {"!isOtherSingleIntegerFixnumArray"})
45984594
public Object zipObjectObjectNotSingleInteger(VirtualFrame frame, RubyArray array, Object[] others) {
4599-
RubyBasicObject proc = RubyArguments.getBlock(frame.getArguments());
4600-
if (proc == null) {
4601-
proc = nil();
4602-
}
4603-
return ruby(frame, "zip_internal(block, *others)", "others", new RubyArray(getContext().getCoreLibrary().getArrayClass(), others, others.length), "block", proc);
4595+
return zipRuby(frame, others);
46044596
}
46054597

46064598
@Specialization(guards = {"!isObject"})
46074599
public Object zipObjectObjectNotObject(VirtualFrame frame, RubyArray array, Object[] others) {
4600+
return zipRuby(frame, others);
4601+
}
4602+
4603+
private Object zipRuby(VirtualFrame frame, Object[] others) {
46084604
RubyBasicObject proc = RubyArguments.getBlock(frame.getArguments());
46094605
if (proc == null) {
46104606
proc = nil();
46114607
}
4612-
return ruby(frame, "zip_internal(block, *others)", "others", new RubyArray(getContext().getCoreLibrary().getArrayClass(), others, others.length), "block", proc);
4608+
return ruby(frame, "zip_internal(*others, &block)", "others", new RubyArray(getContext().getCoreLibrary().getArrayClass(), others, others.length), "block", proc);
46134609
}
46144610

46154611
}

truffle/src/main/ruby/core/rubinius/common/array.rb

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,7 @@ def first(n = undefined)
9191
Array.new self[0, n]
9292
end
9393

94-
# MODIFIED to handle blocks from java
95-
def zip_internal(block, *others)
94+
def zip_internal(*others)
9695
out = Array.new(size) { [] }
9796
others = others.map do |ary|
9897
if ary.respond_to?(:to_ary)
@@ -110,8 +109,8 @@ def zip_internal(block, *others)
110109
others.each { |ary| slot << ary.at(i) }
111110
end
112111

113-
unless block.nil?
114-
out.each { |ary| block.call(ary) }
112+
if block_given?
113+
out.each { |ary| yield ary }
115114
return nil
116115
end
117116

0 commit comments

Comments
 (0)