@RubyLevelMethod(name = "open")
  public static RubyValue open(RubyValue receiver, RubyArray args, RubyBlock block) {
    String filename = args.get(0).toStr();
    RubyIO io;
    if (args.size() <= 1) {
      io = ObjectFactory.createFile(filename, "r");
    } else if (args.get(1) instanceof RubyFixnum) {
      String mode = "r";
      int i = args.get(1).toInt();
      if ((i & RDWR) != 0) {
        mode = mode + "w";
      }
      io = ObjectFactory.createFile(filename, mode);
    } else {
      RubyString mode = (RubyString) args.get(1);
      io = ObjectFactory.createFile(filename, mode.toString());
    }

    if (null == block) {
      return io;
    } else {
      RubyValue v = block.invoke(receiver, io);
      io.close();
      return v;
    }
  }
Beispiel #2
0
  // RHO_COMMENT
  public boolean equals(Object o, boolean bConvToAry) {
    if (this == o) {
      return true;
    }

    if (o instanceof RubyArray) {
      RubyArray that = (RubyArray) o;
      int size = array_.size();
      if (size != that.size()) {
        return false;
      }

      for (int i = 0; i < size; ++i) {
        if (!this.get(i).equals(that.get(i))) {
          return false;
        }
      }

      return true;
    } else if (o instanceof RubyValue && bConvToAry) {
      RubyValue v = (RubyValue) o;
      if (!v.respondTo(RubyID.toAryID)) {
        return false;
      } else {
        return v.equals(this);
      }
    } else {
      return false;
    }
  }
Beispiel #3
0
 // @RubyLevelMethod(name="compact")
 public RubyValue compact() {
   // RHO_COMMENT
   // return copy().compact_danger();
   RubyArray arRes = copy();
   arRes.compact_danger(); // If No nil inside it return NIL
   return arRes;
 }
  @RubyLevelMethod(name = "__load_with_reflection__", module = true)
  public static RubyValue loadWithReflection(RubyValue receiver, RubyValue arg, RubyBlock block) {
    String required_file = arg.toStr();
    String name = NameFactory.createMainClassName(required_file);
    try {
      Class c = Class.forName(name);
      Object o = c.newInstance();
      RubyProgram p = (RubyProgram) o;

      // $".push(file_name) unless $".include?(file_name)
      RubyArray a = (RubyArray) GlobalVariables.get("$\"");
      if (a.include(arg) == RubyConstant.QFALSE) {
        a.push(arg);
      }

      p.invoke();
      return RubyConstant.QTRUE;
    } catch (ClassNotFoundException e) {
      return RubyConstant.QFALSE;
    } catch (InstantiationException e) {
      return RubyConstant.QFALSE;
    } catch (IllegalAccessException e) {
      return RubyConstant.QFALSE;
    }
  }
  // FIXME:This method should be module.
  @RubyLevelMethod(name = "raise", alias = "fail")
  public static RubyValue raise(RubyValue value, RubyArray args) {
    RubyExceptionValue e;
    if (null == args) {
      // With no arguments, raises the exception in $! or raises a RuntimeError if $! is nil.
      RubyValue v = GlobalVariables.get("$!");
      if (RubyConstant.QNIL == v) {
        e = new RubyExceptionValue(RubyRuntime.RuntimeErrorClass, "");
      } else {
        e = (RubyExceptionValue) v;
      }
    } else if (1 == args.size() && (args.get(0) instanceof RubyString)) {
      // With a single String argument, raises a RuntimeError with the string as a message.
      e =
          new RubyExceptionValue(
              RubyRuntime.RuntimeErrorClass, ((RubyString) args.get(0)).toString());
    } else if (args.get(0) instanceof RubyExceptionValue) {
      // Otherwise, the first parameter should be the name of an Exception class
      // (or an object that returns an Exception when sent exception). The optional second
      // parameter sets the message associated with the exception, and the third parameter
      // is an array of callback information.
      e = (RubyExceptionValue) args.get(0);
      if (args.size() > 1) {
        e.setMessage(((RubyString) args.get(1)).toString());
      }
    } else {
      RubyClass v = (RubyClass) args.get(0);
      e = new RubyExceptionValue(v, 1 == args.size() ? "" : ((RubyString) args.get(1)).toString());
    }

    throw new RubyException(e);
  }
Beispiel #6
0
  // @RubyLevelMethod(name="gsub!")
  public RubyValue gsub_danger(RubyArray args, RubyBlock block) {
    if (null == block) {
      RubyString result = gsub(this, args);

      if (result == this) {
        return RubyConstant.QNIL;
      } else {
        return setString(result.toString());
      }
    } else {
      if (null == args || args.size() != 1) {
        int actual_argc = (null == args) ? 0 : args.size();
        throw new RubyException(
            RubyRuntime.ArgumentErrorClass,
            "in `gsub!': wrong number of arguments (" + actual_argc + " for 1)");
      }
      if (!(args.get(0) instanceof RubyRegexp)) {
        throw new RubyException(
            RubyRuntime.ArgumentErrorClass,
            "wrong argument type " + args.get(0).getRubyClass().getName() + " (expected Regexp)");
      }

      RubyRegexp r = (RubyRegexp) args.get(0);
      return setString(r.gsub(this, block).toString());
    }
  }
Beispiel #7
0
  private RubyString sub(RubyString g, RubyArray args) {
    if (null == args || args.size() != 2) {
      int actual_argc = (null == args) ? 0 : args.size();
      throw new RubyException(
          RubyRuntime.ArgumentErrorClass,
          "in `sub': wrong number of arguments (" + actual_argc + " for 2)");
    }

    if (!(args.get(1) instanceof RubyString)) {
      throw new RubyException(
          RubyRuntime.ArgumentErrorClass,
          "can't convert " + args.get(1).getRubyClass().getName() + " into String");
    }

    RubyString s = (RubyString) args.get(1);

    if (args.get(0) instanceof RubyRegexp) {
      RubyRegexp r = (RubyRegexp) args.get(0);
      return r.sub(g, s);
    } else if (args.get(0) instanceof RubyString) {
      RubyString r = (RubyString) args.get(0);
      String result = StringMe.replaceFirst(g.toString(), r.toString(), s.toString());
      return ObjectFactory.createString(result);
    } else {
      throw new RubyException(
          RubyRuntime.ArgumentErrorClass,
          "wrong argument type " + args.get(0).getRubyClass().getName() + " (expected Regexp)");
    }
  }
Beispiel #8
0
  // @RubyLevelMethod(name="split")
  public RubyValue split(RubyArray args) {
    RubyValue r = (null == args) ? GlobalVariables.get("$;") : args.get(0);

    Collection /*<String>*/ splitResult;
    boolean bSkipFirstEmptyItem = false;
    if (r == RubyConstant.QNIL) {
      splitResult = split(this, " ");
    } else if (r instanceof RubyRegexp) {
      RubyRegexp reg = (RubyRegexp) r;
      splitResult = split(this, reg, args);

      if (reg.getPattern().getPattern().startsWith("(?=")) bSkipFirstEmptyItem = true;
    } else if (r instanceof RubyString) {
      splitResult = split(this, ((RubyString) r).toString());
    } else {
      throw new RubyException(
          RubyRuntime.ArgumentErrorClass,
          "wrong argument type " + r.getRubyClass() + " (expected Regexp)");
    }

    RubyArray a = new RubyArray(splitResult.size());
    int i = 0;
    //        for (String str : splitResult) {
    for (Iterator iter = splitResult.iterator(); iter.hasNext(); ) {
      String str = (String) iter.next();
      if (!(bSkipFirstEmptyItem && 0 == i && (str == null || str.equals("")))) {
        // To conform ruby's behavior, discard the first empty element
        a.add(ObjectFactory.createString(str));
      }
      ++i;
    }
    return a;
  }
Beispiel #9
0
 // @RubyLevelMethod(name="indexes", alias="indices")
 public RubyValue indexes(RubyArray args) {
   RubyArray a = new RubyArray(args.size());
   for (int i = 0; i < size(); i++) {
     RubyFixnum n = (RubyFixnum) args.get(i);
     a.add(get(n.toInt()));
   }
   return a;
 }
Beispiel #10
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;
 }
Beispiel #11
0
  // @RubyLevelMethod(name="end_with?")
  public RubyValue opEndWith(RubyArray v) {

    for (int i = 0; i < v.size(); i++) {
      RubyValue res = opEndWith(v.get(i));
      if (res == RubyConstant.QTRUE) return RubyConstant.QTRUE;
    }
    return RubyConstant.QFALSE;
  }
Beispiel #12
0
 private Collection /*<String>*/ split(RubyString g, RubyRegexp r, RubyArray args) {
   if (args.size() <= 1) {
     return r.split(g.toString(), 0);
   } else {
     RubyFixnum i = (RubyFixnum) args.get(1);
     return r.split(g.toString(), i.toInt());
   }
 }
Beispiel #13
0
 private RubyArray minus(RubyArray other) {
   RubyArray a = this.copy();
   //        for (RubyValue v : other) {
   for (Iterator iter = other.iterator(); iter.hasNext(); ) {
     RubyValue v = (RubyValue) iter.next();
     a.remove(v);
   }
   return a;
 }
Beispiel #14
0
  public RubyValue captures() {
    RubyArray ar = new RubyArray();
    for (int i = 1; i < result_.groups(); i++) {
      String res = result_.group(i);
      if (res == null) ar.add(RubyConstant.QNIL);
      else ar.add(ObjectFactory.createString(res));
    }

    return ar;
  }
Beispiel #15
0
  public RubyValue to_a() {
    RubyArray ar = new RubyArray();
    for (int i = 0; i < result_.groups(); i++) {
      String str = result_.group(i);
      if (str != null) ar.add(ObjectFactory.createString(str));
      else ar.add(RubyConstant.QNIL);
    }

    return ar;
  }
Beispiel #16
0
  // @RubyLevelMethod(name="[]=")
  public RubyValue aset(RubyArray args) {
    if (3 == args.size()) {
      int index = args.get(0).toInt();
      int length = args.get(1).toInt();
      return replace(index, length, args.get(2));
    }

    throw new RubyException(
        RubyRuntime.ArgumentErrorClass, "wrong number of arguments (" + args.size() + " for 2)");
  }
Beispiel #17
0
  public RubyArray copy() {
    RubyArray resultArray = new RubyArray(array_.size());
    //        for (RubyValue v : array_) {
    for (Iterator iter = array_.iterator(); iter.hasNext(); ) {
      RubyValue v = (RubyValue) iter.next();
      resultArray.add(v);
    }

    return resultArray;
  }
Beispiel #18
0
  // @RubyLevelMethod(name="replace")
  public RubyValue replace(RubyValue arg) {
    RubyArray anotherArray = (RubyArray) arg;
    if (this == anotherArray) return this;

    clear();
    for (int i = 0; i < anotherArray.size(); i++) {
      add(anotherArray.get(i));
    }
    return this;
  }
Beispiel #19
0
 // @SuppressWarnings("unchecked")
 public /*RubyArray*/ RubyValue clone() {
   RubyArray v =
       new RubyArray(
           0,
           this.rhs_size_,
           this.has_single_asterisk_or_lambda_call_); // (RubyArray) super.clone();
   v.array_ = new ArrayList /*<RubyValue>*/(this.array_);
   v.doClone(this);
   return v;
 }
  @RubyLevelMethod(name = "respond_to?")
  public static RubyValue respond_to(RubyValue receiver, RubyArray args) {
    if (null == args || args.size() < 1) {
      int actual_argc = (null == args) ? 0 : args.size();
      throw new RubyException(
          RubyRuntime.ArgumentErrorClass,
          "in `respond_to': wrong number of arguments (" + actual_argc + " for 1)");
    }

    boolean include_private = (RubyConstant.QTRUE == args.get(1));
    RubyID mid = RubyID.intern(args.get(0).toStr());
    return ObjectFactory.createBoolean(hasMethod(receiver, mid, include_private));
  }
Beispiel #21
0
  // @RubyLevelMethod(name="&")
  public RubyArray and(RubyValue value) {
    RubyArray other = value.toAry();

    RubyArray a = new RubyArray();
    //        for (RubyValue v : array_) {
    for (Iterator iter = array_.iterator(); iter.hasNext(); ) {
      RubyValue v = (RubyValue) iter.next();
      if (other.include(v) == RubyConstant.QTRUE && a.include(v) == RubyConstant.QFALSE) {
        a.add(v);
      }
    }
    return a;
  }
Beispiel #22
0
 private boolean recursiveAdd(RubyArray receiver, RubyArray array) {
   boolean modified = false;
   for (int i = 0; i < array.size(); i++) {
     RubyValue val = array.get(i);
     if (val instanceof RubyArray) {
       modified = true;
       recursiveAdd(receiver, (RubyArray) val);
     } else {
       receiver.add(val);
     }
   }
   return modified;
 }
  @RubyLevelMethod(name = "eval", module = true)
  public static RubyValue eval(RubyValue receiver, RubyArray args) {
    RubyBinding binding = null;
    if (args.get(1) instanceof RubyBinding) {
      binding = (RubyBinding) args.get(1);
    }

    String file_name = null;
    if (args.size() > 2) {
      file_name = args.get(2).toStr();
    }

    return eval(args.get(0).toStr(), binding, file_name);
  }
 public static Object[] buildFormatArg(RubyArray args, int start) {
   Object[] raw_args = new Object[args.size() - start];
   for (int i = 0; i < args.size() - start; ++i) {
     Object v = args.get(i + start);
     if (v instanceof RubyFixnum) {
       raw_args[i] = ((RubyFixnum) v).toInt();
     } else if (v instanceof RubyFloat) {
       raw_args[i] = ((RubyFloat) v).doubleValue();
     } else {
       raw_args[i] = v;
     }
   }
   return raw_args;
 }
Beispiel #25
0
  // create a new Array containing every element from index to the end
  public RubyValue collect(int index) {
    AssertMe.rho_assert(index >= 0);

    final int size = array_.size() - index;
    if (size < 0) {
      return new RubyArray();
    }

    RubyArray a = new RubyArray(size);
    for (int i = index; i < array_.size(); ++i) {
      a.add(array_.get(i));
    }

    return a;
  }
Beispiel #26
0
 private RubyArray plus(RubyArray v) {
   int size = array_.size() + v.size();
   RubyArray resultArray = new RubyArray(size);
   resultArray.array_.addAll(array_);
   resultArray.array_.addAll(v.array_);
   return resultArray;
 }
Beispiel #27
0
  // @RubyLevelMethod(name="[]")
  public RubyValue aref(RubyValue arg) {
    if (arg instanceof RubyFixnum) {
      int index = arg.toInt();
      if (index < 0) index += result_.groups();

      String res = result_.group(index);
      if (res == null) return RubyConstant.QNIL;

      return ObjectFactory.createString(res);
    } else if (arg instanceof RubyString) {
      // TODO: implement m = /(?<foo>a+)b/.match("ccaaab"); m["foo"]   #=> "aaa"
      throw new RuntimeException("Not implemented");
    }

    RubyArray ar = (RubyArray) to_a();
    return ar.aref(arg);
  }
Beispiel #28
0
 // @RubyLevelMethod(name="push")
 public RubyArray multiPush(RubyArray args) {
   //    	for (RubyValue v : args) {
   for (Iterator iter = args.iterator(); iter.hasNext(); ) {
     RubyValue v = (RubyValue) iter.next();
     this.array_.add(v);
   }
   return this;
 }
Beispiel #29
0
 // @RubyLevelMethod(name="chomp!")
 public RubyValue chomp_danger(RubyArray args) {
   RubyValue separator = (null == args) ? GlobalVariables.get("$/") : args.get(0);
   if (chomp(((RubyString) separator).toString())) {
     return this;
   } else {
     return RubyConstant.QNIL;
   }
 }
Beispiel #30
0
  private void processResponse(RubyValue res) {
    if (res != null && res != RubyConstant.QNIL && res instanceof RubyHash) {
      RubyHash resHash = (RubyHash) res;
      RubyValue resBody = null;

      RubyArray arKeys = resHash.keys();
      RubyArray arValues = resHash.values();
      for (int i = 0; i < arKeys.size(); i++) {
        String strKey = arKeys.get(i).toString();
        if (strKey.equals("request-body")) resBody = arValues.get(i);
        else if (strKey.equals("status")) responseCode = arValues.get(i).toInt();
        else if (strKey.equals("message")) responseMsg = arValues.get(i).toString();
        else resHeaders.addProperty(strKey, arValues.get(i).toString());
      }
      String strBody = "";

      if (resBody != null && resBody != RubyConstant.QNIL)
        strBody = resBody.toRubyString().toString();

      if (!RHOCONF().getBool("log_skip_post")) LOG.TRACE(strBody);

      try {
        responseData = new ByteArrayInputStream(strBody.getBytes("UTF-8"));
      } catch (java.io.UnsupportedEncodingException exc) {
        LOG.ERROR("Error getting utf-8 body :", exc);
      }

      if (responseData != null)
        contentLength = Integer.parseInt(resHeaders.getPropertyIgnoreCase("Content-Length"));
    }
  }