昔から、いそがしいとつい飲み食いする量がひどく増えてしまう癖だけは
なおりません。最近体重計に乗るのが怖いです。
なおりません。最近体重計に乗るのが怖いです。
import java.util.ArrayList; import java.util.Map; import org.jruby.Ruby; import org.jruby.RubyClass; import org.jruby.RubyRuntimeAdapter; import org.jruby.internal.runtime.methods.DynamicMethod; import org.jruby.javasupport.JavaembedUtils; import org.jruby.runtime.ThreadContext; import org.jruby.runtime.builtin.IRubyObject; public class JRubyTest { private static class Foo { public String getFoo() { return "foo"; } } public static void main(String[] args) throws Exception { Ruby ruby = JavaembedUtils.initialize(new ArrayList<String>()); RubyRuntimeAdapter evaler = JavaembedUtils.newRuntimeAdapter(); String ls = System.getProperty("line.separator"); String script = "def foo(arg)" + ls + " puts arg.foo" + ls + "end"; evaler.eval(ruby, script); Map<String, DynamicMethod> map = ruby.getClass("Object").getMethods(); DynamicMethod method = map.get("foo"); ThreadContext ctx = ruby.getCurrentContext(); RubyClass obj = ruby.getObject(); IRubyObject arg = JavaembedUtils.javaToRuby(ruby, new Foo()); //[A] IRubyObject rv = method.call(ctx, obj, null, "foo", arg); //[B] System.out.println(rv); } }----
package jp.liq.rest.liq_restlet; import java.util.ArrayList; import org.jruby.Ruby; import org.jruby.RubyRuntimeAdapter; import org.jruby.javasupport.JavaembedUtils; import org.jruby.runtime.builtin.IRubyObject; public class JRubyTest { public static void main(String[] args) throws Exception { Ruby ruby = JavaembedUtils.initialize(new ArrayList<String>()); RubyRuntimeAdapter evaler = JavaembedUtils.newRuntimeAdapter(); String ls = System.getProperty("line.separator"); String script = "def foo" + ls + " return 'bar' " + ls + "end"; evaler.eval(ruby, script); IRubyObject eval = evaler.eval(ruby, "foo"); System.out.println(eval); } }