Skip to content

Instantly share code, notes, and snippets.

@fukayatsu
Created April 26, 2012 12:45
Show Gist options
  • Save fukayatsu/2499321 to your computer and use it in GitHub Desktop.
Save fukayatsu/2499321 to your computer and use it in GitHub Desktop.
チャレンジプログラム4(jRuby)
package main.java;
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 Calc {
public static void main(String[] args) {
// jRubyを使う準備
Ruby runtime = JavaEmbedUtils.initialize(new ArrayList<Object>());
RubyRuntimeAdapter evaler = JavaEmbedUtils.newRuntimeAdapter();
if (args.length == 1) {
// 余計な文字を取り除く
String arg0 = args[0].replaceAll("'", "").replace("\"", "");
// jRubyから結果を受け取る
IRubyObject rubyObject = evaler.eval(runtime, arg0);
// RubyObjectを文字列に変換して出力
System.out.println(rubyObject.toString());
} else {
System.out.println("usage 1: java -jar Calc.jar 1+2*3");
System.out.println("usage 2: java -jar Calc.jar \"1 + 2*/3.0\"");
}
// jRuby runtimeを終了
JavaEmbedUtils.terminate(runtime);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment