Helpers to create a ScriptingContainer and set it up so it lives as well as possible in the OSGi world.
Currently:
TODO: look into using the LoadService of jruby. Look if it would be possible to reuse the base runtime and minimize the cost of new jruby runtimes.
<init>(org.osgi.framework.Bundle creator)A scripting container where the classloader can find classes in the osgi creator bundle and where the jruby home is set to point to the one in the jruby's bundle home folder.
scope: LocalContextScope.SINGLETHREAD; behavior: LocalVariableBehavior.TRANSIENT
<init>(org.osgi.framework.Bundle creator, org.jruby.embed.LocalContextScope scope, org.jruby.embed.LocalVariableBehavior behavior)A scripting container where the classloader can find classes in the osgi creator bundle and where the jruby home is set to point to the one in the jruby's bundle home folder.
| name | type | description |
|---|---|---|
| scope | org.jruby.embed.LocalContextScope | if null, LocalContextScope.SINGLETHREAD |
| behavior | org.jruby.embed.LocalVariableBehavior | if null, LocalVariableBehavior.TRANSIENT |
addToClassPath(org.osgi.framework.Bundle bundle)| name | type | description |
|---|---|---|
| bundle | org.osgi.framework.Bundle | Add a bundle to the jruby classloader.
Equivalent to require "bundle:/#{bundle.symbolic.name}" |
getOSGiBundleClassLoader() · also: get_os_gi_bundle_class_loaderReturns: The ScriptingContainer's classloader casted to a JRubyOSGiBundleClassLoader. It is the parent classloader of the actual's runtime's JRubyClassLoader. It enables finding classes and resources in the OSGi environment.
parse(org.osgi.framework.Bundle bundle, String path, int[] lines)Parses a script given by a input stream and return an object which can be run(). This allows the script to be parsed once and evaluated many times.
| name | type | description |
|---|---|---|
| bundle | org.osgi.framework.Bundle | is where the script is located |
| path | String | is the entry in the bundle. |
| lines | int[] | are linenumbers to display for parse errors and backtraces. This field is optional. Only the first argument is used for parsing. When no line number is specified, 0 is applied to. |
Returns: an object which can be run
runScriptlet(String bundleSymbolicName, String path)| name | type | description |
|---|---|---|
| bundle_symbolic_name | String | The bundle where the script is located. Lazily added to the loader of the OSGiScriptingContainer. (require bundle:/... is implicitly done here) |
| path | String | The entry in the bundle |
runScriptlet(org.osgi.framework.Bundle bundle, String path)| name | type | description |
|---|---|---|
| bundle | org.osgi.framework.Bundle | The bundle where the script is located. Lazily added to the loader of the OSGiScriptingContainer. (require bundle:/... is implicitly done here) |
| path | String | The entry in the bundle |
addClassLoader(ClassLoader classLoader)add the given classLoader to the LOAD_PATH and GEM_PATH
getArgv() · also: get_argvReturns a list of argument.
Returns: an arguments' list.
setArgv(String[] argv) · also: set_argvChanges values of the arguments' list.
| name | type | description |
|---|---|---|
| argv | String[] | a new arguments' list. |
getAttributeMap() · also: get_attribute_mapReturns a attribute map in one of LocalContextScope. Attributes
in this map accept any key value pair, types of which are java.lang.Object.
Ruby scripts do not look up this map.
Returns: an attribute map specific to the current thread
callMethod(Object receiver, String methodName, Object[] args)Executes a method defined in Ruby script. This method is used when a Ruby method does not have any argument.
| name | type | description |
|---|---|---|
| receiver | Object | is an instance that will receive this method call. Ruby's self object will be used if no appropriate receiver is given. |
| method_name | String | is a method name to be called |
| args | Object[] | is an array of method arguments |
Returns: an instance of requested Java type
callMethod(Object receiver, String methodName, org.jruby.runtime.Block block, Object[] args)Executes a method defined in Ruby script. This method is used when a Ruby method does not have any argument.
| name | type | description |
|---|---|---|
| receiver | Object | is an instance that will receive this method call Ruby's self object will be used if no appropriate receiver is given. |
| method_name | String | is a method name to be called |
| block | org.jruby.runtime.Block | is a block to be executed in this method |
| args | Object[] | is an array of method arguments |
Returns: an instance of requested Java type
callMethod(Object receiver, String methodName, Class<T> returnType)Executes a method defined in Ruby script. This method is used when a Ruby method does not have any argument.
| name | type | description |
|---|---|---|
| receiver | Object | is an instance that will receive this method call. Ruby's self object will be used if no appropriate receiver is given. |
| method_name | String | is a method name to be called |
| return_type | Class<T> | is the type we want it to convert to |
Returns: an instance of requested Java type
callMethod(Object receiver, String methodName, Object singleArg, Class<T> returnType)Executes a method defined in Ruby script. This method is used when a Ruby method have only one argument.
| name | type | description |
|---|---|---|
| receiver | Object | is an instance that will receive this method call. Ruby's self object will be used if no appropriate receiver is given. |
| method_name | String | is a method name to be called |
| single_arg | Object | is an method argument |
| return_type | Class<T> | returnType is the type we want it to convert to |
Returns: an instance of requested Java type
callMethod(Object receiver, String methodName, Object[] args, Class<T> returnType)Executes a method defined in Ruby script. This method is used when a Ruby method have multiple arguments.
| name | type | description |
|---|---|---|
| receiver | Object | is an instance that will receive this method call. Ruby's self object will be used if no appropriate receiver is given. |
| method_name | String | is a method name to be called |
| args | Object[] | is an array of method arguments |
| return_type | Class<T> | is the type we want it to convert to |
Returns: an instance of requested Java type
callMethod(Object receiver, String methodName, Object[] args, org.jruby.runtime.Block block, Class<T> returnType)Executes a method defined in Ruby script. This method is used when a Ruby method have multiple arguments, one of which is a block.
| name | type | description |
|---|---|---|
| receiver | Object | is an instance that will receive this method call. Ruby's self object will be used if no appropriate receiver is given. |
| method_name | String | is a method name to be called |
| args | Object[] | is an array of method arguments except a block |
| block | org.jruby.runtime.Block | is a block to be executed in this method |
| return_type | Class<T> | is the type we want it to convert to |
Returns: an instance of requested Java type
callMethod(Object receiver, String methodName, Class<T> returnType, org.jruby.embed.EmbedEvalUnit unit)Executes a method defined in Ruby script. This method is used when a Ruby method does not have any argument, and users want to inject Ruby's local variables' values from Java.
| name | type | description |
|---|---|---|
| receiver | Object | is an instance that will receive this method call. Ruby's self object will be used if no appropriate receiver is given. |
| method_name | String | is a method name to be called |
| return_type | Class<T> | is the type we want it to convert to |
| unit | org.jruby.embed.EmbedEvalUnit | is parsed unit |
Returns: an instance of requested Java type
callMethod(Object receiver, String methodName, Object[] args, Class<T> returnType, org.jruby.embed.EmbedEvalUnit unit)Executes a method defined in Ruby script. This method is used when a Ruby method have multiple arguments, and users want to inject Ruby's local variables' values from Java.
| name | type | description |
|---|---|---|
| receiver | Object | is an instance that will receive this method call. Ruby's self object will be used if no appropriate receiver is given. |
| method_name | String | is a method name to be called |
| args | Object[] | is an array of method arguments |
| return_type | Class<T> | is the type we want it to convert to |
| unit | org.jruby.embed.EmbedEvalUnit | is parsed unit |
Returns: an instance of requested Java type
callMethod(Object receiver, String methodName, Object[] args, org.jruby.runtime.Block block, Class<T> returnType, org.jruby.embed.EmbedEvalUnit unit)Executes a method defined in Ruby script. This method is used when a Ruby method have multiple arguments, one of which is a block, and users want to inject Ruby's local variables' values from Java.
| name | type | description |
|---|---|---|
| receiver | Object | is an instance that will receive this method call. Ruby's self object will be used if no appropriate receiver is given. |
| method_name | String | is a method name to be called |
| args | Object[] | is an array of method arguments except a block |
| block | org.jruby.runtime.Block | is a block to be executed in this method |
| return_type | Class<T> | is the type we want it to convert to |
| unit | org.jruby.embed.EmbedEvalUnit | is parsed unit |
Returns: is the type we want it to convert to
callSuper(Object receiver, Object[] args, Class<T> returnType)| name | type | description |
|---|---|---|
| receiver | Object | is an instance that will receive this method call. Ruby's self object will be used if no appropriate receiver is given. |
| args | Object[] | is an array of method arguments |
| return_type | Class<T> | is the type we want it to convert to |
Returns: is the type we want it to convert to
callSuper(Object receiver, Object[] args, org.jruby.runtime.Block block, Class<T> returnType)| name | type | description |
|---|---|---|
| receiver | Object | is an instance that will receive this method call. Ruby's self object will be used if no appropriate receiver is given. |
| args | Object[] | is an array of method arguments except a block |
| block | org.jruby.runtime.Block | is a block to be executed in this method |
| return_type | Class<T> | is the type we want it to convert to |
Returns: is the type we want it to convert to
getClassLoader() · also: get_class_loaderReturns a class loader object that is currently used. This loader loads Ruby files and libraries.
Returns: a class loader object that is currently used.
setClassLoader(ClassLoader loader) · also: set_class_loaderChanges a class loader to a given loader. Call this method before you use put/get, runScriptlet, and parse methods so that the given class loader will be used.
| name | type | description |
|---|---|---|
| loader | ClassLoader | a new class loader to be set. |
getClassloaderDelegate() · also: get_classloader_delegateRetrieve the self-first classloader setting.
setClassloaderDelegate(boolean classloaderDelegate) · also: set_classloader_delegateForce dynamically-loaded Java classes to load first from the classloader provided by JRuby before searching parent classloaders. This can be used to isolated dependency in different runtimes from each other and from parent classloaders. The default behavior is to defer to parent classloaders if they can provide the requested classes. Note that if different versions of a library are loaded by both a parent classloader and the JRuby classloader, those classess would be incompatible with each other and with other library objects from the opposing classloader.
| name | type | description |
|---|---|---|
| classloader_delegate | boolean | set whether prefer the JRuby classloader to delegate first to the parent classloader when dynamically loading classes |
clear()Removes all of the mappings from this map. The map will be empty after this call returns. Ruby variables are also removed from Ruby instance. However, Ruby instance keep having global variable names with null value. This is a short cut method of ScriptingContainer#getVarMap().clear().
Returns a compile mode currently chosen, which is one of CompileMode.JIT, CompileMode.FORCE, CompileMode.OFF. The default mode is CompileMode.JIT. Also, CompileMode.OFF is chosen when a security restriction is set.
Returns: a compile mode.
setCompileMode(org.jruby.RubyInstanceConfig.CompileMode mode) · also: set_compile_modeChanges a compile mode to a given mode, which should be one of CompileMode.JIT, CompileMode.FORCE, CompileMode.OFF. Call this method before you use put/get, runScriptlet, and parse methods so that the given mode will be used.
| name | type | description |
|---|---|---|
| mode | org.jruby.RubyInstanceConfig.CompileMode | compile mode |
getCurrentDirectory() · also: get_current_directoryReturns a current directory. The default current directory is identical to a value of "user.dir" system property if no security restriction is set. If the "user.dir" directory is protected by the security restriction, the default value is "/".
Returns: a current directory.
setCurrentDirectory(String directory) · also: set_current_directoryChanges a current directory to a given directory. The current directory can be changed anytime.
| name | type | description |
|---|---|---|
| directory | String | a new directory to be set. |
getEnvironment() · also: get_environmentReturns a map of environment variables.
Returns: a map that has environment variables' key-value pairs.
setEnvironment(Map environment) · also: set_environmentChanges an environment variables' map. Call this method before you use put/get, runScriptlet, and parse methods so that initial configurations will work.
| name | type | description |
|---|---|---|
| environment | Map | a new map of environment variables. |
getError() · also: get_errorReturns an error stream assigned to STDERR and $stderr.
Returns: output stream for error stream
setError(java.io.PrintStream pstream) · also: set_errorChanges STDERR and $stderr to a given print stream. The default standard error is java.lang.System.err. Call this method before you use put/get, runScriptlet, and parse methods so that the given print stream will be used.
| name | type | description |
|---|---|---|
| pstream | java.io.PrintStream | a print stream to be set |
setError(java.io.Writer writer) · also: set_errorChanges STDERR and $stderr to a given writer. No writer is set by default. Call this method before you use put/get, runScriptlet, and parse methods so that the given writer will be used.
| name | type | description |
|---|---|---|
| writer | java.io.Writer | a writer to be set |
getErrorWriter() · also: get_error_writerReturns an error writer set in an attribute map.
Returns: an error writer in a attribute map
setErrorWriter(java.io.Writer errorWriter) · also: set_error_writerReplaces a standard error by a specified writer.
| name | type | description |
|---|---|---|
| error_writer | java.io.Writer | is a writer to be set |
finalize()Ensure this ScriptingContainer instance is terminated when nobody holds any
references to it (and GC wants to reclaim it).
Note that org.jruby.embed.LocalContextScope#SINGLETON containers will not terminate on GC.
| Throwable | ||
get(String key)Returns a value of the specified key in a top level of runtime or null if this map doesn't have a mapping for the key. The key must be a valid Ruby variable or constant name.
| name | type | description |
|---|---|---|
| key | String | is a key whose associated value is to be returned |
Returns: a value to which the specified key is mapped, or null if this map contains no mapping for the key
get(Object receiver, String key)Returns a value of a specified key in a specified receiver or null if a variable map doesn't have a mapping for the key in a given receiver. The key must be a valid Ruby variable or constant name. A global variable doesn't depend on the receiver.
| name | type | description |
|---|---|---|
| receiver | Object | a receiver to get the value from |
| key | String | is a key whose associated value is to be returned |
Returns: a value to which the specified key is mapped, or null if this map contains no mapping for the key
getAttribute(Object key)Returns an attribute value associated with the specified key in a attribute map. This is a short cut method of ScriptingContainer#getAttributeMap().get(key).
| name | type | description |
|---|---|---|
| key | Object | is the attribute key |
Returns: value is a value associated to the specified key
getInstance(Object receiver, Class<T> clazz)Returns an instance of a requested interface type. An implementation of the requested interface is done by a Ruby script, which has been evaluated before getting the instance. In most cases, users don't need to use this method. ScriptingContainer's runScriptlet method returns an instance of the interface type that is implemented by Ruby.
Example
Interface
//QuadraticFormula.java
package org.jruby.embed;
import java.util.List;
public interface QuadraticFormula {
List solve(int a, int b, int c) throws Exception;
}
Implementation
#quadratic_formula.rb
def solve(a, b, c)
v = b ** 2 - 4 * a * c
if v < 0: raise RangeError end
s0 = ((-1)*b - Math.sqrt(v))/(2*a)
s1 = ((-1)*b + Math.sqrt(v))/(2*a)
return s0, s1
end
Usage
ScriptingContainer container = new ScriptingContainer();
String filename = "ruby/quadratic_formula_class.rb";
Object receiver = container.runScriptlet(PathType.CLASSPATH, filename);
QuadraticFormula qf = container.getInstance(receiver, QuadraticFormula.class);
try {
List<Double> solutions = qf.solve(1, -2, -13);
printSolutions(solutions);
solutions = qf.solve(1, -2, 13);
for (double s : solutions) {
System.out.print(s + ", ");
}
} catch (Exception e) {
e.printStackTrace();
}
Output
-2.7416573867739413, 4.741657386773941,
| name | type | description |
|---|---|---|
| receiver | Object | is an instance that implements the interface |
| clazz | Class<T> | is a requested interface |
Returns: an instance of a requested interface type
getProperty(String key)Returns an array of values associated to a key.
| name | type | description |
|---|---|---|
| key | String | is a key in a property file |
Returns: values associated to the key
getHomeDirectory() · also: get_home_directoryReturns a JRuby home directory. The default JRuby home is the value of JRUBY_HOME environment variable, or "jruby.home" system property when no security restriction is set to those directories. If none of JRUBY_HOME or jruby.home is set and jruby-complete.jar is used, the default JRuby home is "/META-INF/jruby.home" in the jar archive. Otherwise, "java.io.tmpdir" system property is the default value.
Returns: a JRuby home directory.
setHomeDirectory(String home) · also: set_home_directoryChanges a JRuby home directory to a directory of a given name. Call this method before you use put/get, runScriptlet, and parse methods so that the given directory will be used.
| name | type | description |
|---|---|---|
| home | String | a name of new JRuby home directory. |
getInput() · also: get_inputReturns an input stream assigned to STDIN and $stdin.
Returns: input stream of STDIN and $stdin
setInput(java.io.InputStream istream) · also: set_inputChanges STDIN and $stdin to a given input stream. The default standard input is java.lang.System.in. Call this method before you use put/get, runScriptlet, and parse methods so that the given input stream will be used.
| name | type | description |
|---|---|---|
| istream | java.io.InputStream | an input stream to be set |
setInput(java.io.Reader reader) · also: set_inputChanges STDIN and $stdin to a given reader. No reader is set by default. Call this method before you use put/get, runScriptlet, and parse methods so that the given reader will be used.
| name | type | description |
|---|---|---|
| reader | java.io.Reader | a reader to be set |
getJitLogEvery() · also: get_jit_log_everyReturns the value of n, which means that jitted methods are logged in every n methods. The default value is 0.
Returns: a value that determines how often jitted methods are logged.
setJitLogEvery(int logEvery) · also: set_jit_log_everyChanges a value of n, so that jitted methods are logged in every n methods. The default value is 0. This value can be set by the jruby.jit.logEvery System property. Call this method before you use put/get, runScriptlet, and parse methods so that the configurations will work.
| name | type | description |
|---|---|---|
| log_every | int | a new number of methods. |
getJitMax() · also: get_jit_maxReturns a value of a max class cache size. The default value is 0 when security restriction is applied, or 4096 when no security restriction exists.
Returns: a value of a max class cache size.
setJitMax(int max) · also: set_jit_maxChanges a value of a max class cache size. The default value is 0 when security restriction is applied, or 4096 when no security restriction exists. This value can be set by jruby.jit.max System property. Call this method before you use put/get, runScriptlet, and parse methods so that the configurations will work.
| name | type | description |
|---|---|---|
| max | int | a new value of a max class cache size. |
getJitMaxSize() · also: get_jit_max_sizeReturns a value of a max size of the bytecode generated by compiler. The default value is -1 when security restriction is applied, or 10000 when no security restriction exists.
Returns: a value of a max size of the bytecode.
setJitMaxSize(int maxSize) · also: set_jit_max_sizeChanges a value of a max size of the bytecode generated by compiler. The default value is -1 when security restriction is applied, or 10000 when no security restriction exists. This value can be set by jruby.jit.maxsize System property. Call this method before you use put/get, runScriptlet, and parse methods so that the configurations will work.
| name | type | description |
|---|---|---|
| max_size | int | a new value of a max size of the bytecode. |
getJitThreshold() · also: get_jit_thresholdReturns a value of the threshold that determines whether jitted methods' call reached to the limit or not. The default value is -1 when security restriction is applied, or 50 when no security restriction exists.
Returns: a value of the threshold.
setJitThreshold(int threshold) · also: set_jit_thresholdChanges a value of the threshold that determines whether jitted methods' call reached to the limit or not. The default value is -1 when security restriction is applied, or 50 when no security restriction exists. This value can be set by jruby.jit.threshold System property. Call this method before you use put/get, runScriptlet, and parse methods so that the configurations will work.
| name | type | description |
|---|---|---|
| threshold | int | a new value of the threshold. |
Returns a value of KCode currently used. The default value is KCode.NONE.
Returns: a KCode value.
Changes a value of KCode to a given value. The value should be one of KCode.NONE, KCode.UTF8, KCode.SJIS, or KCode.EUC. The default value is KCode.NONE. Call this method before you use put/get, runScriptlet, and parse methods so that the given value will be used.
| name | type | description |
|---|---|---|
| kcode | org.jruby.util.KCode | a new KCode value. |
getLoadPaths() · also: get_load_pathsReturns a list of load paths for Ruby scripts/libraries. If no paths is given, the list is created from java.class.path System property.
Returns: a list of load paths.
setLoadPaths(List<String> paths) · also: set_load_pathsChanges a list of load paths Ruby scripts/libraries. The default value is an empty array. If no paths is given, the list is created from java.class.path System property. This value can be set by org.jruby.embed.class.path System property, also. Call this method before you use put/get, runScriptlet, and parse methods so that the given paths will be used.
| name | type | description |
|---|---|---|
| paths | List<String> | a new list of load paths. |
getLoadServiceCreator() · also: get_load_service_creatorReturns a LoadServiceCreator currently used.
Returns: a current LoadServiceCreator.
setLoadServiceCreator(org.jruby.RubyInstanceConfig.LoadServiceCreator creator) · also: set_load_service_creatorChanges a LoadServiceCreator to a given one. Call this method before you use put/get, runScriptlet, and parse methods so that initial configurations will work.
| name | type | description |
|---|---|---|
| creator | org.jruby.RubyInstanceConfig.LoadServiceCreator | a new LoadServiceCreator |
setNativeEnabled(boolean b) · also: set_native_enabledSet whether native code is enabled for this config. Disabling it also disables C extensions (@see RubyInstanceConfig#setCextEnabled).
| name | type | description |
|---|---|---|
| b | boolean | new value indicating whether native code is enabled |
isNativeEnabled() · also: is_native_enabledGet whether native code is enabled for this config.
Returns: true if native code is enabled; false otherwise.
Returns an instance of EmbedRubyObjectAdapter for embedders to invoke
methods defined by Ruby. The script must be evaluated prior to a method call.
In most cases, users don't need to use this method. ScriptingContainer's
callMethods are the shortcut and work in the same way.
Example
# calendar.rb
require 'date'
class Calendar
def initialize;@today = DateTime.now;end
def next_year;@today.year + 1;end
end
Calendar.new
ScriptingContainer container = new ScriptingContainer();
String filename = "ruby/calendar.rb";
Object receiver = instance.runScriptlet(PathType.CLASSPATH, filename);
EmbedRubyObjectAdapter adapter = instance.newObjectAdapter();
Integer result =
(Integer) adapter.callMethod(receiver, "next_year", Integer.class);
System.out.println("next year: " + result);
System.out.println(instance.get("@today"));
Outputs:
next year: 2010
2009-05-19T17:46:44-04:00
Returns: an instance of EmbedRubyObjectAdapter
Returns an instance of EmbedRubyRuntimeAdapter for embedders to parse
scripts.
Returns: an instance of EmbedRubyRuntimeAdapter.
setObjectSpaceEnabled(boolean enable) · also: set_object_space_enabledChanges the value to determine whether the Object Space is enabled or not. The default value is false. Call this method before you use put/get, runScriptlet, and parse methods so that the given condition will be used.
| name | type | description |
|---|---|---|
| enable | boolean | true to enable the Object Space, or false to disable. |
isObjectSpaceEnabled() · also: is_object_space_enabledTests whether the Object Space is enabled or not.
Returns: true if the Object Space is able to use, otherwise, false.
getOutput() · also: get_outputReturns an output stream assigned to STDOUT and $stdout.
Returns: an output stream of STDOUT and $stdout
setOutput(java.io.PrintStream pstream) · also: set_outputChanges STDOUT and $stdout to a given output stream. The default standard output is java.lang.System.out. Call this method before you use put/get, runScriptlet, and parse methods so that the given output stream will be used.
| name | type | description |
|---|---|---|
| pstream | java.io.PrintStream | an output stream to be set |
setOutput(java.io.Writer writer) · also: set_outputChanges STDOUT and $stdout to a given writer. No writer is set by default. Call this method before you use put/get, runScriptlet, and parse methods so that the given writer will be used.
| name | type | description |
|---|---|---|
| writer | java.io.Writer | a writer to be set |
Parses a script and return an object which can be run(). This allows the script to be parsed once and evaluated many times.
| name | type | description |
|---|---|---|
| script | String | is a Ruby script to be parsed |
| lines | int[] | are linenumbers to display for parse errors and backtraces. This field is optional. Only the first argument is used for parsing. When no line number is specified, 0 is applied to. |
Returns: an object which can be run
parse(java.io.Reader reader, String filename, int[] lines)Parses a script given by a reader and return an object which can be run(). This allows the script to be parsed once and evaluated many times.
| name | type | description |
|---|---|---|
| reader | java.io.Reader | is used to read a script from |
| filename | String | is used as in information, for example, appears in a stack trace of an exception |
| lines | int[] | are linenumbers to display for parse errors and backtraces. This field is optional. Only the first argument is used for parsing. When no line number is specified, 0 is applied to. |
Returns: an object which can be run
parse(org.jruby.embed.PathType type, String filename, int[] lines)Parses a script read from a specified path and return an object which can be run(). This allows the script to be parsed once and evaluated many times.
| name | type | description |
|---|---|---|
| type | org.jruby.embed.PathType | is one of the types PathType defines |
| filename | String | is used as in information, for example, appears in a stack trace of an exception |
| lines | int[] | are linenumbers to display for parse errors and backtraces. This field is optional. Only the first argument is used for parsing. When no line number is specified, 0 is applied to. |
Returns: an object which can be run
parse(java.io.InputStream istream, String filename, int[] lines)Parses a script given by a input stream and return an object which can be run(). This allows the script to be parsed once and evaluated many times.
| name | type | description |
|---|---|---|
| istream | java.io.InputStream | is an input stream to get a script from |
| filename | String | filename is used as in information, for example, appears in a stack trace of an exception |
| lines | int[] | are linenumbers to display for parse errors and backtraces. This field is optional. Only the first argument is used for parsing. When no line number is specified, 0 is applied to. |
Returns: an object which can be run
Returns a Profile currently used. The default value is Profile.DEFAULT, which has the same behavior to Profile.ALL. Profile allows you to define a restricted subset of code to be loaded during the runtime initialization. When you use JRuby in restricted environment such as Google App Engine, Profile is a helpful option.
Returns: a current profiler.
Changes a Profile to a given one. The default value is Profile.DEFAULT, which has the same behavior to Profile.ALL. Call this method before you use put/get, runScriptlet, and parse methods so that initial configurations will work. Profile allows you to define a restricted subset of code to be loaded during the runtime initialization. When you use JRuby in restricted environment such as Google App Engine, Profile is a helpful option. For example, Profile.NO_FILE_CLASS doesn't load File class.
| name | type | description |
|---|---|---|
| profile | org.jruby.Profile | a new profiler to be set. |
Returns currently configured ProfileOutput object, which determines where the output of profiling operations will be sent. (e.g., a file specified by --profile.out).
Returns: current profiling output location.
setProfileOutput(org.jruby.runtime.profile.builtin.ProfileOutput out) · also: set_profile_outputChanges ProfileOutput to given one. The default value is a ProfileOutput instance that writes to stderr. Similar to passing `--profile.out` from the command line
| name | type | description |
|---|---|---|
| out | org.jruby.runtime.profile.builtin.ProfileOutput | a new ProfileOutput object, to which profiling data should be written |
getProfilingMode() · also: get_profiling_modeReturns a ProfilingMode currently used. The default value is ProfilingMode.OFF.
Returns: a current profiling mode.
setProfilingMode(org.jruby.RubyInstanceConfig.ProfilingMode mode) · also: set_profiling_modeChanges a ProfilingMode to a given one. The default value is Profiling.OFF. Call this method before you use put/get, runScriptlet, and parse methods so that initial configurations will work. ProfilingMode allows you to change profiling style. Profiling.OFF - default. profiling off. Profiling.API - activates Ruby profiler API. equivalent to --profile.api command line option Profiling.FLAT - synonym for --profile command line option equivalent to --profile.flat command line option Profiling.GRAPH - runs with instrumented (timed) profiling, graph format. equivalent to --profile.graph command line option.
| name | type | description |
|---|---|---|
| mode | org.jruby.RubyInstanceConfig.ProfilingMode | a new profiling mode to be set. |
Returns a provider instance of LocalContextProvider. When users
want to configure Ruby runtime, they can do by setting class loading paths,
org.jruby.RubyInstanceConfig to the provider before they get Ruby
runtime.
Returns: a provider of LocalContextProvider
put(String key, Object value)Associates the specified value with the specified key in a variable map. This key-value pair is injected to a top level of runtime during evaluation. If the map previously contained a mapping for the key, the old value is replaced. The key must be a valid Ruby variable or constant name. It will be a top level variable or constant.
| name | type | description |
|---|---|---|
| key | String | is a key that the specified value is to be associated with |
| value | Object | is a value to be associated with the specified key |
Returns: a previous value associated with a key, or null if there was no mapping for this key.
put(Object receiver, String key, Object value)Associates the specified value with the specified key in a variable map. This key-value pair is injected to a given receiver during evaluation. If the map previously contained a mapping for the key, the old value is replaced. The key must be a valid Ruby variable or constant name. A given receiver limits the scope of a variable or constant. However, a global variable is accessible globally always.
| name | type | description |
|---|---|---|
| receiver | Object | a receiver to put the value in |
| key | String | is a key that the specified value is to be associated with |
| value | Object | is a value to be associated with the specified key |
Returns: a previous value associated with a key, or null if there was no mapping for this key.
getReader() · also: get_readerReturns a reader set in an attribute map.
Returns: a reader in an attribute map
setReader(java.io.Reader reader) · also: set_readerReplaces a standard input by a specified reader
| name | type | description |
|---|---|---|
| reader | java.io.Reader | is a reader to be set |
getRecordSeparator() · also: get_record_separatorReturns a record separator. The default value is "\n".
Returns: a record separator.
setRecordSeparator(String separator) · also: set_record_separatorChanges a record separator to a given value. If "0" is given, the record separator goes to "\n\n", "777" goes to "", otherwise, an octal value of the given number. Call this before you use put/get, runScriptlet, and parse methods so that initial configurations will work.
| name | type | description |
|---|---|---|
| separator | String | a new record separator value, "0" or "777" |
remove(String key)Removes the specified Ruby variable with the specified variable name from a variable map and runtime top level. If the map previously contained a mapping for the key, the old value is returned. The key must be a valid Ruby variable name.
| name | type | description |
|---|---|---|
| key | String | is a key that the specified value is to be associated with |
Returns: a previous value associated with a key, or null if there was no mapping for this key.
remove(Object receiver, String key)Removes the specified Ruby variable with the specified variable name in a variable map and given receiver. If the map previously contained a mapping for the key, the old value is returned. The key must be a valid Ruby variable name. This is a short cut method of ScriptingContainer#getVarMap().remove(key).
| name | type | description |
|---|---|---|
| receiver | Object | a receiver to remove the value from |
| key | String | is a key that the specified value is to be associated with |
Returns: a previous value associated with a key, or null if there was no mapping for this key.
removeAttribute(Object key)Removes the specified value with the specified key in a attribute map. If the map previously contained a mapping for the key, the old value is returned. This is a short cut method of ScriptingContainer#getAttributeMap().remove(key).
| name | type | description |
|---|---|---|
| key | Object | is a key that the specified value is to be removed from |
Returns: the previous value associated with key, or null if there was no mapping for key.
resetErrorWriter()resetWriter()setRunRubyInProcess(boolean inprocess) · also: set_run_ruby_in_processChanges the value to determine whether Ruby runs in a process or not. The default value is true. Call this method before you use put/get, runScriptlet, and parse methods so that the given condition will be set.
| name | type | description |
|---|---|---|
| inprocess | boolean | true when Ruby is set to run in the process, or false not to run in the process. |
isRunRubyInProcess() · also: is_run_ruby_in_processTests whether Ruby runs in a process or not.
Returns: true if Ruby is configured to run in a process, otherwise, false.
runRubyMethod(Class<T> returnType, Object receiver, String methodName, Object[] args)Executes a method defined in Ruby script. This method is used when a Ruby method does not have any argument.
| name | type | description |
|---|---|---|
| return_type | Class<T> | is the type we want it to convert to |
| receiver | Object | is an instance that will receive this method call. The receiver can be null or other Java objects as well as RubyObject. The null will be converted to RubyNil. Java objects will be wrapped in RubyObject. |
| method_name | String | is a method name to be called |
| args | Object[] | is an array of method arguments |
Returns: an instance of requested Java type
runRubyMethod(Class<T> returnType, Object receiver, String methodName, org.jruby.runtime.Block block, Object[] args)Executes a method defined in Ruby script. This method is used when a Ruby method does not have any argument.
| name | type | description |
|---|---|---|
| return_type | Class<T> | is the type we want it to convert to |
| receiver | Object | is an instance that will receive this method call. The receiver can be null or other Java objects as well as RubyObject. The null will be converted to RubyNil. Java objects will be wrapped in RubyObject. |
| method_name | String | is a method name to be called |
| block | org.jruby.runtime.Block | is an optional Block object. Send null for no block. |
| args | Object[] | is an array of method arguments |
Returns: an instance of requested Java type
runScriptlet(String script)Evaluates a script under the current scope (perhaps the top-level scope) and returns a result only if a script returns a value. Right after the parsing, the script is evaluated once.
| name | type | description |
|---|---|---|
| script | String | is a Ruby script to get run |
Returns: an evaluated result converted to a Java object
runScriptlet(java.io.Reader reader, String filename)Evaluates a script read from a reader under the current scope (perhaps the top-level scope) and returns a result only if a script returns a value. Right after the parsing, the script is evaluated once.
| name | type | description |
|---|---|---|
| reader | java.io.Reader | is used to read a script from |
| filename | String | is used as in information, for example, appears in a stack trace of an exception |
Returns: an evaluated result converted to a Java object
runScriptlet(java.io.InputStream istream, String filename)Evaluates a script read from a input stream under the current scope (perhaps the top-level scope) and returns a result only if a script returns a value. Right after the parsing, the script is evaluated once.
| name | type | description |
|---|---|---|
| istream | java.io.InputStream | is used to input a script from |
| filename | String | is used as in information, for example, appears in a stack trace of an exception |
Returns: an evaluated result converted to a Java object
runScriptlet(org.jruby.embed.PathType type, String filename)Reads a script file from specified path and evaluates it under the current scope (perhaps the top-level scope) and returns a result only if a script returns a value. Right after the parsing, the script is evaluated once.
| name | type | description |
|---|---|---|
| type | org.jruby.embed.PathType | is one of the types PathType defines |
| filename | String | is used to read the script from and an information |
Returns: an evaluated result converted to a Java object
getScriptFilename() · also: get_script_filenameReturns a script filename to run. The default value is "<script>".
Returns: a script filename.
setScriptFilename(String filename) · also: set_script_filenameChanges a script filename to run. The default value is "<script>". Call this before you use put/get, runScriptlet, and parse methods so that initial configurations will work.
| name | type | description |
|---|---|---|
| filename | String | a new script filename. |
setAttribute(Object key, Object value)Associates the specified value with the specified key in a attribute map. If the map previously contained a mapping for the key, the old value is replaced. This is a short cut method of ScriptingContainer#getAttributeMap().put(key, value).
| name | type | description |
|---|---|---|
| key | Object | is a key that the specified value is to be associated with |
| value | Object | is a value to be associated with the specified key |
Returns: the previous value associated with key, or null if there was no mapping for key.
getSupportedRubyVersion() · also: get_supported_ruby_versionReturns version information about JRuby and Ruby supported by this platform.
Returns: version information.
terminate()Cleanly shut down this ScriptingContainer and any JRuby resources it holds. All ScriptingContainer instances should be terminated when you are done with them, rather then leaving them for GC to finalize.
Returns a variable map in one of LocalContextScope. Variables
in this map is used to share between Java and Ruby. Map keys are Ruby's
variable names, thus they must be valid Ruby names.
Returns: a variable map specific to the current thread
getWriter() · also: get_writerReturns a writer set in an attribute map.
Returns: a writer in a attribute map
setWriter(java.io.Writer writer) · also: set_writerReplaces a standard output by a specified writer.
| name | type | description |
|---|---|---|
| writer | java.io.Writer | is a writer to be set |