Add ruby interop

This commit is contained in:
R. Tyler Croy 2015-07-25 14:27:42 -07:00
parent 5ef39e87c6
commit 118865e028
No known key found for this signature in database
GPG Key ID: 1426C7DC3F51E16F
3 changed files with 26 additions and 1 deletions

View File

@ -8,7 +8,7 @@ buildscript {
apply plugin: 'com.github.jruby-gradle.base'
import com.github.jrubygradle.JRubyExec
defaultTasks 'runServer'
defaultTasks 'runInterop'
/* Disabling the default repositories so we can ues the new (unreleased)
* default rubygems proxy
@ -42,6 +42,10 @@ task runDemo(type: JRubyExec) {
script './demo.rb'
}
task runInterop(type: JRubyExec) {
script './rubyinterop.rb'
}
task runServer(type: JRubyExec) {
script './server.rb'
systemProperties 'java.library.path' : "${buildDir}/native"

2
rubyinterop.js Normal file
View File

@ -0,0 +1,2 @@
print(example);
print(example.callMethod('hello'));

19
rubyinterop.rb Normal file
View File

@ -0,0 +1,19 @@
#!/usr/bin/env ruby
require 'java'
java_import 'javax.script.ScriptEngineManager'
java_import 'javax.script.ScriptContext'
class Example
def hello
return 'hello world from Ruby'
end
end
engine = ScriptEngineManager.new.get_engine_by_name('nashorn')
engine.get_bindings(ScriptContext::ENGINE_SCOPE).put('example', Example.new)
puts '-' * 30
engine.eval(File.open('rubyinterop.js').read)
puts '-' * 30