WARNING: This server provides a static reference view of the NetKernel documentation. Links to dynamic content do not work. For the best experience we recommend you install NetKernel and view the documentation in the live system .

Running Ruby Scripts

To run a Ruby script just request the active:ruby runtime with an operator argument referencing the script. All scripts are dynamically transrepted (compiled) for JIT optimisation.

Here's an example...

SOURCE active:ruby+operator@res:/hello.rb

Where res:/hello.rb is

#Test hello world
import org.netkernel.layer0.nkf.INKFResponse
a= "Hello From Ruby"
print a
resp=$context.createResponseFrom(a);
resp.setExpiry(INKFResponse::EXPIRY_ALWAYS);

$context

As with all other languages on NetKernel, every script execution is provided with a unique $context object. Which is an instance of the INKFRequestContext and provides full access to the NKF API.

Ruby on ROC

Having Ruby in the ROC domain means that your solution can treat the ROC address space as an extensible dynamic location for your regular Ruby class libraries. To tell the Ruby runtime about these you implement the res:/etc/RUBYPATH resource in your applications address space.

Full Ruby 1.8. ClassLibrary in the ROC domain

If you installed the companion package lang-ruby-lib you will have the full Ruby 1.8 classlibrary as a NetKernel module. The module contains the class library and mounts it into the ROC address space at

res:/lib/ruby/1.8/  

To use the library, simply import the module urn:org:netkernel:lang:ruby:lib into your application space near your import for the main urn:org:netkernel:lang:ruby ruby runtime.

Then provide a res:/etc/RUBYPATH entry for it...

res:/etc/RUBYPATH

The active:ruby instance will source res:/etc/RUBYPATH from the local address space. This is a text file containing an ROC base URI identifier for the location of libraries. Each line must be a separate searchable URI path. Here's an example

#Use the Ruby 1.8 library
res:/lib/ruby/1.8/
#Use my local application classes
res:/myapp/ruby/
#Use some common team classes from another module
res:/common/project/pisces/ruby/

Calling Java from Ruby

Since JRuby is written in Java, you have the power to use all of the existing Java infrastructure including Java classes in your module's classpath.

A detailed reference is provided here

https://github.com/jruby/jruby/wiki/CallingJavaFromJRuby

Here's some common examples...

Import Java Class

import org.netkernel.layer0.nkf.INKFResponse

Include a complete Java Package

 module Layer0Reps
	 include_package 'org.netkernel.layer0.representation'
 end

Use the package from the Ruby module like this...

Layer0Reps::IHDSNode

Reference Static Field in a Java Class

INKFResponse::EXPIRY_ALWAYS

Reference a Java class

java.lang.String.java_class

Typical example...

rep=$context.source("res:/helloworld.txt",java.lang.String.java_class) 

Detailed Example

Here's an example that uses the Ruby class library and returns a response on the $context object...

#Require standard Ruby 1.8 modules.
require 'benchmark'
require 'uri'

#Import Java INKFResponse 
import org.netkernel.layer0.nkf.INKFResponse
        
n = 50
Benchmark.bm(7) do |x|
    x.report("for:")   { for i in 1..n; a = "1"; end }
    x.report("times:") { n.times do   ; a = "1"; end }
    x.report("upto:")  { 1.upto(n) do ; a = "1"; end }
end
   
s="Hello From Ruby"
result = URI.escape(s)
    
print result
    
if(result=="Hello%20From%20Ruby")
    result=s
end
    
    
resp=$context.createResponseFrom(result);
resp.setExpiry(INKFResponse::EXPIRY_ALWAYS);