@JRubyMethod(name = "_id2ref", required = 1, module = true, visibility = Visibility.PRIVATE) public static IRubyObject id2ref(IRubyObject recv, IRubyObject id) { Ruby runtime = id.getRuntime(); if (!(id instanceof RubyFixnum)) { throw recv.getRuntime().newTypeError(id, recv.getRuntime().getFixnum()); } RubyFixnum idFixnum = (RubyFixnum) id; long longId = idFixnum.getLongValue(); if (longId == 0) { return runtime.getFalse(); } else if (longId == 2) { return runtime.getTrue(); } else if (longId == 4) { return runtime.getNil(); } else if (longId % 2 != 0) { // odd return runtime.newFixnum((longId - 1) / 2); } else { if (runtime.isObjectSpaceEnabled()) { IRubyObject object = runtime.getObjectSpace().id2ref(longId); if (object == null) { return runtime.getNil(); } return object; } else { runtime .getWarnings() .warn("ObjectSpace is disabled; _id2ref only supports immediates, pass -X+O to enable"); return runtime.getNil(); } } }
private static RubyArray coerceResult( final Ruby runtime, final IRubyObject result, final boolean err) { if (!(result instanceof RubyArray) || ((RubyArray) result).getLength() != 2) { if (err) throw runtime.newTypeError("coerce must return [x, y]"); return null; } else if (!result.isNil()) { RubyWarnings warnings = runtime.getWarnings(); warnings.warn("Bad return value for #coerce, called by numerical comparison operators."); warnings.warn("#coerce must return [x, y]. The next release will raise an error for this."); } return (RubyArray) result; }