// Collection of code snippets by Arne Vajhøj 
// (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) 
import org.jruby.embed.LocalVariableBehavior;
import org.jruby.embed.ScriptingContainer;

public class EmbeddedJRuby {
   public static class Data {
        public int iv;
        public String sv;
        public Data(int iv, String sv) {
            this.iv = iv;
            this.sv = sv;
        }
        public void m() {
            iv++;
            sv += "X";
        }
        @Override
        public String toString() {
            return String.format("(%d,%s)", iv, sv);
        }
    }
	public static void main(String[] args) {
		ScriptingContainer cont = new ScriptingContainer(LocalVariableBehavior.PERSISTENT);
		cont.put("n", 3);
		cont.put("o", new Data(123, "ABC"));
		cont.runScriptlet("for i in 1..n do\r\n" +
                          "    puts 'Hi from Ruby!'\r\n" +
                          "end\r\n" +
                          "o.iv = o.iv + 1\r\n" +
                          "o.sv = o.sv + 'X'\r\n" +
                          "o.m()\r\n");
		Data o = (Data)cont.get("o");
		System.out.println(o);
	}
}
