@RubyLevelMethod(name = "p", module = true)
 public static RubyValue p(RubyValue receiver, RubyValue arg) {
   RubyValue str = RubyAPI.callNoArgMethod(arg, null, RubyID.inspectID);
   RubyString value = str.toRubyString();
   value.appendString("\n");
   System.out.print(value.toString());
   return RubyConstant.QNIL;
 }
  @RubyLevelMethod(name = "extend")
  public static RubyValue extend(RubyValue receiver, RubyArray args) {
    for (RubyValue v : args) {
      RubyAPI.callPublicOneArgMethod(v, receiver, null, RubyID.extendObjectID);
    }

    return receiver;
  }
示例#3
0
 // @RubyLevelMethod(name="collect!")
 public RubyValue collect_danger(RubyBlock block) {
   RubyArray a = (RubyArray) RubyAPI.callPublicNoArgMethod(this, block, RubyID.intern("collect"));
   clear();
   for (int i = 0; i < a.size(); i++) {
     add(a.get(i));
   }
   return this;
 }
 @RubyLevelMethod(name = "===")
 public static RubyValue objEqual(RubyValue receiver, RubyValue arg) {
   if (receiver == arg) {
     return RubyConstant.QTRUE;
   } else {
     boolean result = RubyAPI.callPublicOneArgMethod(receiver, arg, null, RubyID.equalID).isTrue();
     return ObjectFactory.createBoolean(result);
   }
 }
示例#5
0
  private RubyValue compare(RubyArray other_array) {
    int length = (size() <= other_array.size()) ? size() : other_array.size();
    for (int i = 0; i < length; ++i) {
      RubyValue v =
          RubyAPI.callPublicOneArgMethod(get(i), other_array.get(i), null, RubyID.unequalID);
      if (!RubyAPI.testEqual(v, ObjectFactory.FIXNUM0)) {
        return v;
      }
    }

    if (size() == other_array.size()) {
      return ObjectFactory.FIXNUM0;
    } else if (size() > other_array.size()) {
      return ObjectFactory.FIXNUM1;
    } else {
      return ObjectFactory.FIXNUM_NEGATIVE_ONE;
    }
  }
示例#6
0
  public RubyString appendString(RubyValue v) {
    if (v instanceof RubyString) {
      return appendString((RubyString) v);
    } else {
      RubyValue r = RubyAPI.callPublicNoArgMethod(v, null, RubyID.toSID);
      if (r instanceof RubyString) return appendString((RubyString) r);

      return ObjectFactory.createString(r.toString());
    }
  }
示例#7
0
  // @RubyLevelMethod(name="*")
  public RubyValue run(RubyValue arg, RubyBlock block) {
    if (arg instanceof RubyFixnum) {
      RubyFixnum times = (RubyFixnum) arg;
      return times(times.toInt());
    } else if (arg instanceof RubyString) {
      return RubyAPI.callOneArgMethod(this, arg, block, RubyID.joinID);
    }

    throw new RubyException(
        RubyRuntime.TypeErrorClass, "no implicit conversion from " + arg + " to integer");
  }
示例#8
0
  // @RubyLevelMethod(name="include?")
  public RubyValue include(RubyValue v) {
    //        for (RubyValue value : array_) {
    for (Iterator iter = array_.iterator(); iter.hasNext(); ) {
      RubyValue value = (RubyValue) iter.next();
      if (RubyAPI.testEqual(value, v)) {
        return RubyConstant.QTRUE;
      }
    }

    return RubyConstant.QFALSE;
  }
示例#9
0
  @RubyLevelMethod(name = "==")
  public RubyValue opEqual(RubyValue arg) {
    if (arg == this) {
      return RubyConstant.QTRUE;
    }

    if (arg instanceof RubyFixnum) {
      return ObjectFactory.createBoolean(this.value_ == ((RubyFixnum) arg).value_);
    }

    return RubyAPI.callOneArgMethod(arg, this, null, RubyID.equalID);
  }
示例#10
0
  public int appendString2(RubyValue v) {
    RubyString str = null;
    if (v instanceof RubyString) {
      str = (RubyString) v;
    } else {
      RubyValue r = RubyAPI.callPublicNoArgMethod(v, null, RubyID.toSID);
      str = (RubyString) r;
    }

    appendString(str);

    return str.length();
  }
示例#11
0
 // @RubyLevelMethod(name="=~")
 public RubyValue operator_match(RubyValue arg) {
   if (arg instanceof RubyRegexp) {
     RubyRegexp reg = (RubyRegexp) arg;
     int p = reg.matchPosition(toString());
     if (p >= 0) {
       return ObjectFactory.createFixnum(p);
     } else {
       return RubyConstant.QNIL;
     }
   } else {
     return RubyAPI.callPublicOneArgMethod(arg, this, null, RubyID.matchID);
   }
 }
示例#12
0
  // @RubyLevelMethod(name="uniq!")
  public RubyValue uniq_danger() {
    boolean b = false;
    for (int i = 0; i < array_.size(); ++i) {
      for (int j = i + 1; j < array_.size(); ) {
        if (RubyAPI.testEqual((RubyValue) array_.get(i), (RubyValue) array_.get(j))) {
          array_.remove(j);
          b = true;
        } else {
          ++j;
        }
      }
    }
    if (!b) return RubyConstant.QNIL;

    return this;
    // return b ? this : RubyConstant.QNIL;
  }
  @RubyLevelMethod(name = "catch", module = true)
  public static RubyValue catchMethod(RubyValue receiver, RubyValue arg, RubyBlock block) {
    if (!(arg instanceof RubySymbol)) {
      throw new RubyException(RubyRuntime.ArgumentErrorClass, arg.toString() + " is not a symbol");
    }

    try {
      block.invoke(receiver);
    } catch (RubyException e) {
      RubyValue ev = RubyAPI.convertRubyException2RubyValue(e);
      if (ev instanceof RubyExceptionValueForThrow) {
        RubyExceptionValueForThrow v = (RubyExceptionValueForThrow) ev;
        if (v.isSameSymbol(arg)) {
          return v.getReturnValue();
        }
      }
      throw e;
    }

    return RubyConstant.QNIL;
  }
 @RubyLevelMethod(name = "kind_of?", alias = "is_a?")
 public static RubyValue kindOf(RubyValue receiver, RubyValue arg) {
   return ObjectFactory.createBoolean(RubyAPI.isKindOf(arg, receiver));
 }
 @RubyLevelMethod(name = "send", alias = "__send__")
 public static RubyValue send(
     RubyValue receiver, RubyValue arg0, RubyValue arg1, RubyBlock block) {
   RubyID mid = RubyID.intern(arg0.toStr());
   return RubyAPI.callOneArgMethod(receiver, arg1, block, mid);
 }
 @RubyLevelMethod(name = "send", alias = "__send__")
 public static RubyValue send(RubyValue receiver, RubyArray args, RubyBlock block) {
   RubyValue method_name = args.delete_at(0);
   RubyID mid = RubyID.intern(method_name.toStr());
   return RubyAPI.callMethod(receiver, args, block, mid);
 }
 @RubyLevelMethod(name = "extend")
 public static RubyValue extend(RubyValue receiver, RubyValue arg) {
   RubyAPI.callPublicOneArgMethod(arg, receiver, null, RubyID.extendObjectID);
   return receiver;
 }