JRubyの require の検索は Javaのクラスパスを用いる
require 'erb'
とある場合は、Javaのクラスパスのデフォルトパッケージ内に、erb.rb が含まれていれば良い。
require 'com/xxx/mylib'
とある場合は、Javaのクラスパスのcom.xxxパッケージ内に、mylib.rb が含まれていれば良い。
public static void main(String[] args) throws FileNotFoundException {
scriptHelloWorld();
scriptHelloWorld2();
scriptHelloWorldFromFile();
}
private static void scriptHelloWorld() {
ScriptingContainer container = new ScriptingContainer();
container.runScriptlet("puts \"Hello World!\"");
}
private static void scriptHelloWorld2() {
ScriptingContainer container = new ScriptingContainer(LocalContextScope.SINGLETHREAD, LocalVariableBehavior.PERSISTENT);
container.runScriptlet("word = 'hello JRuby!'");
container.runScriptlet("puts word");
}
private static void scriptHelloWorldFromFile() throws FileNotFoundException {
ScriptingContainer container = new ScriptingContainer();
container.runScriptlet(new FileInputStream(new File("helloworld.rb")), "");
}
#配列
array = Array.new
#ハッシュ
hash = Hash.new
hash['a'] = 'A'
# A が表示される
puts hash['a']
最終更新:2010年11月16日 15:10