Skip to content

Instantly share code, notes, and snippets.

@godfat
Created August 17, 2009 10:11
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 godfat/169032 to your computer and use it in GitHub Desktop.
Save godfat/169032 to your computer and use it in GitHub Desktop.
// 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(); }
}
}
// 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;
}
}
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment