Skip to content

Instantly share code, notes, and snippets.

@xli
Created May 30, 2009 09:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xli/120454 to your computer and use it in GitHub Desktop.
Save xli/120454 to your computer and use it in GitHub Desktop.
import static org.jruby.javasupport.JavaEmbedUtils.invokeMethod;
import org.jruby.Ruby;
import org.jruby.runtime.builtin.IRubyObject;
public class JRubyObject {
public static JRubyObject load(Ruby runtime, String dump) {
return new JRubyObject(((IRubyObject) marshalClass(runtime).send("load", dump)));
}
private static JRubyObject marshalClass(Ruby runtime) {
return new JRubyObject(runtime.evalScriptlet("Marshal"));
}
private final IRubyObject obj;
public JRubyObject(IRubyObject obj) {
this.obj = obj;
}
@SuppressWarnings("unchecked")
public <T> T send(String method, Object... args) {
Object[] nonAdapterArgs = new Object[args.length];
for (int i = 0; i < args.length; i++) {
nonAdapterArgs[i] = getRidOfAdapter(args[i]);
}
return (T) invokeMethod(obj.getRuntime(), obj, method, nonAdapterArgs, Object.class);
}
@Override
public String toString() {
return this.obj.toString();
}
private Object getRidOfAdapter(Object object) {
if (object instanceof JRubyObject) {
return ((JRubyObject) object).obj;
}
return object;
}
public String dump() {
return marshalClass(obj.getRuntime()).send("dump", obj);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment