Example #1
0
  protected Block prepareBlock(
      ThreadContext context, IRubyObject self, DynamicScope currDynScope, Object[] temp) {
    if (closure == null) return Block.NULL_BLOCK;

    Object value = closure.retrieve(context, self, currDynScope, temp);

    Block b = null;
    if (value instanceof Block) b = (Block) value;
    else if (value instanceof RubyProc) b = ((RubyProc) value).getBlock();
    else if (value instanceof RubyMethod)
      b = ((RubyProc) ((RubyMethod) value).to_proc(context, null)).getBlock();
    else if ((value instanceof IRubyObject) && ((IRubyObject) value).isNil()) b = Block.NULL_BLOCK;
    else if (value instanceof IRubyObject)
      b =
          ((RubyProc)
                  TypeConverter.convertToType(
                      (IRubyObject) value, context.getRuntime().getProc(), "to_proc", true))
              .getBlock();
    else
      throw new RuntimeException(
          "Unhandled case in CallInstr:prepareBlock.  Got block arg: " + value);

    // Blocks passed in through calls are always normal blocks, no matter where they came from
    b.type = Block.Type.NORMAL;
    return b;
  }
Example #2
0
  public static RubyArray convertToRubyArrayWithCoerce(Ruby runtime, IRubyObject value) {
    if (value instanceof RubyArray) return ((RubyArray) value);

    IRubyObject newValue = TypeConverter.convertToType(value, runtime.getArray(), "to_ary", false);

    if (newValue.isNil()) {
      return RubyArray.newArrayLight(runtime, value);
    }

    // empirically it appears that to_ary coersions always return array or nil, so this
    // should always be an array by now.
    return (RubyArray) newValue;
  }
  @JRubyMethod(visibility = PRIVATE)
  @Override
  public IRubyObject initialize_copy(IRubyObject other) {
    RubyStringIO otherIO =
        (RubyStringIO)
            TypeConverter.convertToType(other, getRuntime().getClass("StringIO"), "to_strio");

    if (this == otherIO) return this;

    data = otherIO.data;
    if (otherIO.isTaint()) setTaint(true);

    return this;
  }
Example #4
0
  @JRubyMethod(visibility = PRIVATE)
  public IRubyObject initialize_copy(ThreadContext context, IRubyObject other) {
    StringIO otherIO =
        (StringIO)
            TypeConverter.convertToType(other, context.runtime.getClass("StringIO"), "to_strio");

    if (this == otherIO) return this;

    ptr = otherIO.ptr;
    infectBy(otherIO);
    flags &= ~STRIO_READWRITE;
    flags |= otherIO.flags & STRIO_READWRITE;

    return this;
  }
Example #5
0
  public static RubyArray convertToRubyArrayWithCoerce(Ruby runtime, IRubyObject value) {
    if (value instanceof RubyArray) return ((RubyArray) value);

    IRubyObject newValue = TypeConverter.convertToType(value, runtime.getArray(), "to_ary", false);

    if (newValue.isNil()) {
      return RubyArray.newArrayLight(runtime, value);
    }

    // must be array by now, or error
    if (!(newValue instanceof RubyArray)) {
      throw runtime.newTypeError(newValue.getMetaClass() + "#" + "to_ary" + " should return Array");
    }

    return (RubyArray) newValue;
  }
Example #6
0
  @JRubyMethod(visibility = Visibility.PRIVATE)
  public IRubyObject initialize_copy(IRubyObject other) {

    RubyStringIO otherIO =
        (RubyStringIO)
            TypeConverter.convertToType(other, getRuntime().fastGetClass("StringIO"), "to_strio");

    if (this == otherIO) {
      return this;
    }

    pos = otherIO.pos;
    lineno = otherIO.lineno;
    eof = otherIO.eof;
    closedRead = otherIO.closedRead;
    closedWrite = otherIO.closedWrite;
    internal = otherIO.internal;
    modes = otherIO.modes;
    if (otherIO.isTaint()) {
      setTaint(true);
    }

    return this;
  }