66
66
import org .jruby .util .ByteList ;
67
67
import org .jruby .util .RubyDateFormatter ;
68
68
import org .jruby .runtime .Helpers ;
69
+ import org .jruby .util .TypeConverter ;
69
70
70
71
import static org .jruby .RubyComparable .invcmp ;
71
72
import static org .jruby .runtime .Helpers .invokedynamic ;
@@ -243,49 +244,32 @@ public static DateTimeZone getTimeZoneFromUtcOffset(Ruby runtime, IRubyObject ut
243
244
return dtz ;
244
245
}
245
246
246
- /* time.c num_exact
247
- */
247
+ // mri: time.c num_exact
248
248
private static IRubyObject numExact (Ruby runtime , IRubyObject v ) {
249
249
IRubyObject tmp ;
250
- switch (v .getMetaClass ().getRealClass ().getClassIndex ()) {
251
- case FIXNUM :
252
- case BIGNUM :
253
- return v ;
254
- case RATIONAL :
255
- break ;
256
- case STRING :
257
- case NIL :
258
- exactTypeError (runtime , v );
259
- default :
250
+ if (v instanceof RubyFixnum || v instanceof RubyBignum ) return v ;
251
+ if (v .isNil ()) exactTypeError (runtime , v );
252
+ if (!(v instanceof RubyRational )) { // Default unknown
253
+ if (v .respondsTo ("to_r" )) {
260
254
tmp = v .callMethod (runtime .getCurrentContext (), "to_r" );
261
- if (!tmp .eql (UNDEF )) {
262
- if (v .respondsTo ("to_str" )) {
263
- exactTypeError (runtime , v );
264
- }
265
- v = tmp ;
266
- break ;
267
- }
268
-
269
- tmp = v .callMethod (runtime .getCurrentContext (), "to_int" );
270
- if (!tmp .isNil ()) {
271
- v = tmp ;
272
- break ;
273
- }
274
- exactTypeError (runtime , v );
255
+ // WTF is this condition for? It responds to to_r and makes something which thinks it is a String?
256
+ if (tmp != null && v .respondsTo ("to_str" )) exactTypeError (runtime , v );
257
+ } else {
258
+ tmp = TypeConverter .checkIntegerType (runtime , v , "to_int" );
259
+ if (tmp .isNil ()) exactTypeError (runtime , v );
260
+ }
261
+ v = tmp ;
275
262
}
276
263
277
- switch (v .getMetaClass ().getRealClass ().getClassIndex ()) {
278
- case FIXNUM :
279
- case BIGNUM :
280
- return v ;
281
- case RATIONAL :
282
- RubyRational r = (RubyRational )v ;
283
- if (r .denominator (runtime .getCurrentContext ()) == RubyFixnum .newFixnum (runtime , 1 )) {
284
- return r .numerator (runtime .getCurrentContext ());
285
- }
286
- break ;
287
- default :
288
- exactTypeError (runtime , v );
264
+ if (v instanceof RubyFixnum || v instanceof RubyBignum ) {
265
+ return v ;
266
+ } else if (v instanceof RubyRational ) {
267
+ RubyRational r = (RubyRational ) v ;
268
+ if (r .denominator (runtime .getCurrentContext ()) == RubyFixnum .newFixnum (runtime , 1 )) {
269
+ return r .numerator (runtime .getCurrentContext ());
270
+ }
271
+ } else {
272
+ exactTypeError (runtime , v );
289
273
}
290
274
291
275
return v ;
@@ -731,8 +715,8 @@ private IRubyObject inspectCommon(DateTimeFormatter formatter, DateTimeFormatter
731
715
@ JRubyMethod
732
716
@ Override
733
717
public RubyArray to_a () {
734
- return getRuntime ().newArrayNoCopy (new IRubyObject [] { sec (), min (), hour (), mday (), month (),
735
- year (), wday (), yday (), isdst (), zone () });
718
+ return getRuntime ().newArrayNoCopy (new IRubyObject []{ sec (), min (), hour (), mday (), month (),
719
+ year (), wday (), yday (), isdst (), zone ()});
736
720
}
737
721
738
722
@ JRubyMethod
@@ -810,7 +794,7 @@ public RubyInteger year() {
810
794
811
795
@ JRubyMethod
812
796
public RubyInteger wday () {
813
- return getRuntime ().newFixnum ((dt .getDayOfWeek ()% 7 ));
797
+ return getRuntime ().newFixnum ((dt .getDayOfWeek () % 7 ));
814
798
}
815
799
816
800
@ JRubyMethod
@@ -832,7 +816,8 @@ public IRubyObject subsec() {
832
816
@ JRubyMethod (name = {"gmt_offset" , "gmtoff" , "utc_offset" })
833
817
public RubyInteger gmt_offset () {
834
818
int offset = dt .getZone ().getOffset (dt .getMillis ());
835
- return getRuntime ().newFixnum (offset /1000 );
819
+
820
+ return getRuntime ().newFixnum (offset / 1000 );
836
821
}
837
822
838
823
@ JRubyMethod (name = {"isdst" , "dst?" })
@@ -998,7 +983,7 @@ public RubyTime round(ThreadContext context, IRubyObject[] args) {
998
983
public static IRubyObject s_new (IRubyObject recv , IRubyObject [] args , Block block ) {
999
984
Ruby runtime = recv .getRuntime ();
1000
985
RubyTime time = new RubyTime (runtime , (RubyClass ) recv , new DateTime (getLocalTimeZone (runtime )));
1001
- time .callInit (args ,block );
986
+ time .callInit (args , block );
1002
987
return time ;
1003
988
}
1004
989
0 commit comments