File tree Expand file tree Collapse file tree 3 files changed +15
-2
lines changed
src/main/java/org/truffleruby/core/numeric Expand file tree Collapse file tree 3 files changed +15
-2
lines changed Original file line number Diff line number Diff line change @@ -18,6 +18,7 @@ Bug fixes:
18
18
* Fix ` Range#bsearch ` and raise ` TypeError ` when range boundaries are non-numeric and block not passed (@andrykonchin ).
19
19
* Fix using the ` --cpusampler ` profiler when there are custom unblock functions for ` rb_thread_call_without_gvl() ` (#3013 , @eregon ).
20
20
* Fix recursive raising ` FrozenError ` exception when redefined ` #inspect ` modifies an object (#3388 , @andrykonchin ).
21
+ * Fix ` Integer#div ` returning the wrong object type when the divisor is a ` Rational ` (@simonlevasseur , @nirvdrum ).
21
22
22
23
Compatibility:
23
24
Original file line number Diff line number Diff line change 143
143
-> { @bignum . div ( -0 ) } . should raise_error ( ZeroDivisionError )
144
144
end
145
145
end
146
+
147
+ context "rational" do
148
+ it "returns self divided by the given argument as an Integer" do
149
+ 2 . div ( 6 /5 r) . should == 1
150
+ 1 . div ( 6 /5 r) . should == 0
151
+ 5 . div ( 6 /5 r) . should == 4
152
+ end
153
+ end
146
154
end
Original file line number Diff line number Diff line change 49
49
import org .truffleruby .language .NoImplicitCastsToLong ;
50
50
import org .truffleruby .language .NotProvided ;
51
51
import org .truffleruby .language .RubyBaseNode ;
52
+ import org .truffleruby .language .RubyGuards ;
52
53
import org .truffleruby .language .WarnNode ;
53
54
import org .truffleruby .language .control .RaiseException ;
54
55
import org .truffleruby .language .dispatch .DispatchNode ;
@@ -495,15 +496,18 @@ public abstract static class IDivNode extends CoreMethodArrayArgumentsNode {
495
496
@ Specialization
496
497
Object idiv (Object a , Object b ,
497
498
@ Cached InlinedConditionProfile zeroProfile ,
498
- @ Cached FloatToIntegerNode floatToIntegerNode ) {
499
+ @ Cached FloatToIntegerNode floatToIntegerNode ,
500
+ @ Cached DispatchNode floorNode ) {
499
501
Object quotient = divNode .executeDiv (a , b );
500
502
if (quotient instanceof Double ) {
501
503
if (zeroProfile .profile (this , (double ) b == 0.0 )) {
502
504
throw new RaiseException (getContext (), coreExceptions ().zeroDivisionError (this ));
503
505
}
504
506
return floatToIntegerNode .execute (this , Math .floor ((double ) quotient ));
505
- } else {
507
+ } else if ( RubyGuards . isRubyInteger ( quotient )) {
506
508
return quotient ;
509
+ } else {
510
+ return floorNode .call (quotient , "floor" );
507
511
}
508
512
}
509
513
You can’t perform that action at this time.
0 commit comments