godfat (owner)

Revisions

gist: 169032 Download_button fork
public
Public Clone URL: git://gist.github.com/169032.git
Embed All Files: show embed
JRuby.java #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// https://scripting.dev.java.net/servlets/ProjectDocumentList
// javac JRuby.java
// java -cp .:jruby-engine.jar:jruby.jar JRuby rloader.rb Loader # => 40
// java -cp .:jruby-engine.jar:jruby.jar JRuby rloader.rb Loader2 # => 80
 
import javax.script.ScriptContext;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
 
class JRuby{
  static public void main(String[] args){
    ScriptEngineManager m = new ScriptEngineManager();
    ScriptEngine jruby = m.getEngineByName("jruby");
    ScriptContext context = jruby.getContext();
    context.setAttribute("input", "20", ScriptContext.ENGINE_SCOPE);
 
    try{
      jruby.eval("require '" + args[0] + "'");
 
      System.out.println(
        jruby.eval(args[1] + ".apply($input)", context));
 
    }
    catch (ScriptException e){ e.printStackTrace(); }
  }
}
 
 
Loader.java #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// javac -Xlint:unchecked Loader.java
// jar -cf example.jar Loader.class
// java Loader example.jar Loader # => 40
// java Loader example.jar Loader2 # => 80
 
class Loader{
  static public void main(String[] args) throws
    ClassNotFoundException,
    NoSuchMethodException,
    IllegalAccessException,
    java.lang.reflect.InvocationTargetException
  {
    // Class<?> c = ClassLoader.getSystemClassLoader().loadClass(args[0]);
 
    java.net.URL[] urls = new java.net.URL[1];
    urls[0] = ClassLoader.getSystemResource(args[0]);
    Class<?> c = new java.net.URLClassLoader(urls).loadClass(args[1]);
 
    System.out.println(
      c.getMethod("apply", String.class).invoke(null, "20"));
  }
 
  static public int apply(String i){
    return Integer.parseInt(i) * 2;
  }
}
 
class Loader2{
  static public int apply(String i){
    return Integer.parseInt(i) * 4;
  }
}
 
 
rloader.rb #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
module Loader
  module_function
  def apply i
    i.to_i * 2
  end
end
 
module Loader2
  module_function
  def apply i
    i.to_i * 4
  end
end