private IRubyObject callEachType(
     MethodType type, IRubyObject rubyReceiver, String methodName, Block block, Object... args) {
   Ruby runtime = rubyReceiver.getRuntime();
   IRubyObject[] rubyArgs = null;
   if (args != null && args.length > 0) {
     rubyArgs = JavaUtil.convertJavaArrayToRuby(runtime, args);
     for (int i = 0; i < rubyArgs.length; i++) {
       IRubyObject obj = rubyArgs[i];
       if (obj instanceof JavaObject) {
         rubyArgs[i] = Java.wrap(runtime, obj);
       }
     }
   }
   ThreadContext context = runtime.getCurrentContext();
   switch (type) {
     case CALLMETHOD_NOARG:
       return RuntimeHelpers.invoke(context, rubyReceiver, methodName);
     case CALLMETHOD:
       return RuntimeHelpers.invoke(context, rubyReceiver, methodName, rubyArgs);
     case CALLMETHOD_WITHBLOCK:
       return RuntimeHelpers.invoke(context, rubyReceiver, methodName, rubyArgs, block);
     case CALLSUPER:
       return RuntimeHelpers.invokeSuper(context, rubyReceiver, rubyArgs, Block.NULL_BLOCK);
     case CALLSUPER_WITHBLOCK:
       return RuntimeHelpers.invokeSuper(context, rubyReceiver, rubyArgs, block);
     default:
       break;
   }
   return null;
 }
Beispiel #2
0
 @JRubyMethod(name = "min", frame = true, compat = CompatVersion.RUBY1_9)
 public IRubyObject min(ThreadContext context, Block block) {
   if (block.isGiven()) {
     return RuntimeHelpers.invokeSuper(context, this, block);
   } else {
     int c = RubyComparable.cmpint(context, begin.callMethod(context, "<=>", end), begin, end);
     if (c > 0 || (c == 0 && isExclusive)) return context.getRuntime().getNil();
     return begin;
   }
 }
Beispiel #3
0
 @JRubyMethod(frame = true, meta = true)
 public static IRubyObject inherited(
     ThreadContext context, IRubyObject recv, IRubyObject subclass) {
   IRubyObject subJavaClass = RuntimeHelpers.invoke(context, subclass, "java_class");
   if (subJavaClass.isNil()) {
     subJavaClass = RuntimeHelpers.invoke(context, recv, "java_class");
     RuntimeHelpers.invoke(context, subclass, "java_class=", subJavaClass);
   }
   return RuntimeHelpers.invokeSuper(context, recv, subclass, Block.NULL_BLOCK);
 }
Beispiel #4
0
 @JRubyMethod(name = "max", frame = true, compat = CompatVersion.RUBY1_9)
 public IRubyObject max(ThreadContext context, Block block) {
   if (block.isGiven() || isExclusive && !(end instanceof RubyNumeric)) {
     return RuntimeHelpers.invokeSuper(context, this, block);
   } else {
     int c = RubyComparable.cmpint(context, begin.callMethod(context, "<=>", end), begin, end);
     Ruby runtime = context.getRuntime();
     if (isExclusive) {
       if (!(end instanceof RubyInteger))
         throw runtime.newTypeError("cannot exclude non Integer end value");
       if (c == 0) return runtime.getNil();
       if (end instanceof RubyFixnum)
         return RubyFixnum.newFixnum(runtime, ((RubyFixnum) end).getLongValue() - 1);
       return end.callMethod(context, "-", RubyFixnum.one(runtime));
     }
     return end;
   }
 }
Beispiel #5
0
 @JRubyMethod(
     name = {"include?", "member?"},
     frame = true,
     compat = CompatVersion.RUBY1_9)
 public IRubyObject include_p19(ThreadContext context, IRubyObject obj) {
   Ruby runtime = context.getRuntime();
   if (begin instanceof RubyNumeric
       || end instanceof RubyNumeric
       || !TypeConverter.convertToTypeWithCheck(begin, runtime.getInteger(), "to_int").isNil()
       || !TypeConverter.convertToTypeWithCheck(end, runtime.getInteger(), "to_int").isNil()) {
     if (rangeLe(context, begin, obj) != null) {
       if (isExclusive) {
         if (rangeLt(context, obj, end) != null) return runtime.getTrue();
       } else {
         if (rangeLe(context, obj, end) != null) return runtime.getTrue();
       }
     }
     return runtime.getFalse();
   } else if (begin instanceof RubyString
       && end instanceof RubyString
       && ((RubyString) begin).getByteList().realSize == 1
       && ((RubyString) end).getByteList().realSize == 1) {
     if (obj.isNil()) return runtime.getFalse();
     if (obj instanceof RubyString) {
       ByteList Vbytes = ((RubyString) obj).getByteList();
       if (Vbytes.realSize != 1) return runtime.getFalse();
       int v = Vbytes.bytes[Vbytes.begin] & 0xff;
       ByteList Bbytes = ((RubyString) begin).getByteList();
       int b = Bbytes.bytes[Bbytes.begin] & 0xff;
       ByteList Ebytes = ((RubyString) end).getByteList();
       int e = Ebytes.bytes[Ebytes.begin] & 0xff;
       if (Encoding.isAscii(v) && Encoding.isAscii(b) && Encoding.isAscii(e)) {
         if ((b <= v && v < e) || (!isExclusive && v == e)) return runtime.getTrue();
         return runtime.getFalse();
       }
     }
   }
   return RuntimeHelpers.invokeSuper(context, this, obj, Block.NULL_BLOCK);
 }