Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

‘eval’ Method

eval(String script)

This method will evaluate a Ruby code from a string and return the result.

Code Block
languagetext
$ruby.eval("puts 'Hello World!'")

eval(String script, String bindingName, Object bindingObject)

This method will evaluate a Ruby code with a single binding object and specified binding name. The code will be evaluated from a string. The binding name will be used as the name for this object.

Code Block
languagetext
#foreach ($class in $Class)
$ruby.eval('"Class name is " + c.name', 'c', $class)
#end

eval(String script, Map bindingMap)

This method will evaluate a Ruby code with a set of binding arguments (a name and an object). The code will be evaluated from a string. The binding map consists of key-value pairs for the binding name and binding object.

Code Block
languagetext
#set ($dict = $map.createHashMap())
#set ($void = $dict.put("first", "foo"))
#set ($void = $dict.put("last", "bar"))
$ruby.eval("puts first + last", $dict)

Another alternative is as follows:

Code Block
languagetext
$ruby.eval("puts first + last", {"first":"foo", "last":"bar"})

The second code contains curly brackets; “{“ and “}” characters, which are not allowed to be used in any RTF template. For the RTF template, use the first code instead.

‘execute’ Method

execute(String filename)

This method will execute a Ruby file. The ‘filename’ parameter is a name of the Ruby file or an absolute path to the Ruby file.

After executing the Ruby file, the result of the execution will be stored in the single context for each report generation. If the Ruby file contains Ruby functions, you can recall the functions with the use of ‘eval()’ methods.

For example, File ‘String.rb’:

Code Block
languagetext
def deCamelCase(str)
return str.gsub!(/(.)([A-Z])/,'\1 \2')
end

Indicated below is the template code.

Code Block
languagetext
$ruby.execute("String.rb")
#foreach ($c in $Class)
$ruby.eval('deCamelCase($c.name)')
#end

execute(String filename, String bindingName, Object bindingObject)

This method will execute a Ruby file with a single binding object and specified binding name. The ‘filename’ parameter is a file path to the Ruby file. The binding name will be used as the name for this object.

Code Block
languagetext
File 'filename.rb'
puts c.name

The template code

Code Block
languagetext
#foreach ($c in $Class)
$ruby.execute("filename.rb", 'c', $c)
#end

execute(String filename, Map bindingMap)

This method will execute a Ruby file with a set of binding arguments (a name and an object). The ‘filename’ parameter is a file path to the Ruby file. The binding map consists of key-value pairs for the binding name and binding object.

File 'filename.rb'

Code Block
languagetext
puts first+' '+last

The template code

Code Block
languagetext
#set ($dict = $map.createHashMap())
#set ($void = $dict.put("first", "foo"))
#set ($void = $dict.put("last", "bar"))
$ruby.execute("filename.rb", $dict)


Info
  • Absolute Path
    If the ‘filename’ is provided with an absolute path, Ruby tool will read the Ruby file from an absolute location such as $ruby.execute(‘c:/mycode/readclass.rb’).
  • Relative Path
    If the ‘filename’ is provided with a relative path, Ruby tool will read the template from a relative location. This relative location starts from the current directory location of the template, such as $ruby.execute(‘readclass.rb’).