Home
Categories
Dictionary
Download
Project Details
Changes Log
FAQ
License

Ruby scripts



Ruby scripts are a type of scripts which can be used in the library. Ruby scripts use the JRuby library.

Script syntax creation

By default you must implement all the methods of the script interface.

  • You don't need to implement any method to get the Script context. The framework will take care of it under the hood

Ruby standard library

By default the ruby standard library is expected to be in stdlib directory which is in the same directory as the jruby.jar libray. For example:
      jruby.jar
      stdlib
      ---- bigdecimal
      ---- cgi
      -----...
However, it is possible to use another directory by using the static method RubyScriptWrapper.setStdLibPath(File).

Importing java libraries

Since JRuby 9.4, the import declaration does not work anymore, you must now use java_import instead.

For example the following script is invalid:
      import 'java.util.List'

      def execute(list)
         return list.size()
      end
You must use instead:
      java_import 'java.util.List'

      def execute(list)
         return list.size()
      end

Loading scripts inside a script

You can load other Ruby scripts inside the script you use. For example suppose you have the script interface:
      public interface Script {
        public int execute();
      }
And the following support.rb Ruby script:
      def give()
        return 10;
      end
And finally the Ruby script implementing the interface:
      load 'support.rb'

      def execute()
        return give()
      end
Executing this script will return 10.

Example

Suppose the following Script interface:
      public interface Script {
        public int execute();
      }
To create the wrapper, you must use a RubyScriptWrapper:
      ScriptWrapper<Script> wrapper = new RubyScriptWrapper<Script>(){
      };
      Script script = wrapper.getScript();

      File file = new File(<our script file>);
      wrapper.installScript(file);

      int value = script.execute();
You can use for example the following script:
      def execute()
        return 10;
      end

Optimization level


The JRuby engine does not support any optimization level.

See also


Categories: impls

Copyright 2019-2020 Herve Girod. All Rights Reserved. Documentation and source under the BSD licence