Skip to content

Commit cb36ca2

Browse files
authored
Merge pull request #7653 from k77ch7/ruby-3.1-fix-strunct-values_at
Fix Struct#values_at when passed an integer Range argument for edge cases
2 parents 8d55d54 + cf568f6 commit cb36ca2

File tree

3 files changed

+17
-17
lines changed

3 files changed

+17
-17
lines changed

core/src/main/java/org/jruby/RubyRange.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1233,8 +1233,8 @@ public static IRubyObject rangeBeginLength(ThreadContext context, IRubyObject ra
12331233
IRubyObject _beg = sites.begin.call(context, range, range);
12341234
IRubyObject _end = sites.end.call(context, range, range);
12351235
boolean excludeEnd = sites.exclude_end.call(context, range, range).isTrue();
1236-
int beg = _beg.convertToInteger().getIntValue();
1237-
int end = _end.convertToInteger().getIntValue();
1236+
int beg = _beg.isNil() ? 0 : _beg.convertToInteger().getIntValue();
1237+
int end = _end.isNil() ? -1 :_end.convertToInteger().getIntValue();
12381238
int origBeg = beg;
12391239
int origEnd = end;
12401240

core/src/main/java/org/jruby/RubyStruct.java

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -828,35 +828,37 @@ private IRubyObject aset(int idx, IRubyObject value) {
828828
return values[newIdx] = value;
829829
}
830830

831-
// NOTE: copied from RubyArray, both RE, Struct, and Array should share one impl
832831
@JRubyMethod(rest = true)
833832
public IRubyObject values_at(IRubyObject[] args) {
833+
final Ruby runtime = metaClass.runtime;
834834
final int olen = values.length;
835835
RubyArray result = getRuntime().newArray(args.length);
836836

837837
for (int i = 0; i < args.length; i++) {
838838
final IRubyObject arg = args[i];
839-
if ( arg instanceof RubyFixnum ) {
839+
if (arg instanceof RubyFixnum ) {
840840
result.append( aref(arg) );
841841
continue;
842842
}
843843

844-
final int[] begLen;
845-
if ( ! ( arg instanceof RubyRange ) ) {
846-
// do result.append
847-
}
848-
else if ( ( begLen = ((RubyRange) args[i]).begLenInt(olen, 0) ) == null ) {
849-
continue;
850-
}
851-
else {
844+
final int [] begLen = new int[2];
845+
if (arg instanceof RubyRange &&
846+
RubyRange.rangeBeginLength(runtime.getCurrentContext(),(RubyRange) args[i], olen, begLen, 1).isTrue()) {
852847
final int beg = begLen[0];
853848
final int len = begLen[1];
854-
for (int j = 0; j < len; j++) {
855-
result.append( aref(j + beg) );
849+
int end = olen < beg + len ? olen : beg + len;
850+
int j;
851+
for (j = beg; j < end; j++) {
852+
result.push(aref(j));
853+
}
854+
if ( beg + len > j ) {
855+
IRubyObject [] tmp = new IRubyObject[beg + len - j];
856+
Helpers.fillNil(tmp, getRuntime());
857+
result.push(tmp);
856858
}
857859
continue;
858860
}
859-
result.append( aref(RubyNumeric.num2int(arg)) );
861+
result.push(aref(RubyNumeric.num2int(arg)));
860862
}
861863

862864
return result;

spec/tags/ruby/core/struct/values_at_tags.txt

Lines changed: 0 additions & 2 deletions
This file was deleted.

0 commit comments

Comments
 (0)