experimenting with more java interop

This commit is contained in:
Marc Peabody
2010-09-28 09:38:41 -04:00
parent 45523c03c0
commit 9b97fea75a
7 changed files with 65 additions and 0 deletions

View File

@@ -10,3 +10,7 @@ task :test do
ruby 'path_to_enlightenment.rb'
end
task :java do
`javac **/*.java`
end

View File

@@ -85,5 +85,15 @@ class AboutJavaInterop < EdgeCase::Koan
assert_equal __(false), java_array.toString.is_a?(java.lang.String)
end
def test_call_custom_class
number_generator = com.edgecase.JavaStuff.new
assert_equal __(true), number_generator.random_number > 0
end
# FREAKS OUT
# def test_call_ruby_to_java_to_ruby
# number_generator = com.edgecase.JavaToRuby.new
# puts number_generator.random_number
# puts "*"*20
# end
end

Binary file not shown.

View File

@@ -0,0 +1,18 @@
package com.edgecase;
import java.util.Random;
class JavaStuff{
public static final Random GENERATOR = new Random(System.currentTimeMillis());
public static final int DEFAULT_MAX = 100;
public JavaStuff(){}
public int randomNumber(){
return randomNumber(DEFAULT_MAX);
}
public int randomNumber(int max){
return GENERATOR.nextInt(max) + 1;
}
}

Binary file not shown.

View File

@@ -0,0 +1,28 @@
package com.edgecase;
import org.jruby.Ruby;
import java.lang.reflect.Method;
import java.util.Map;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
class JavaToRuby{
public static final Ruby RUBY_RUNTIME = Ruby.getDefaultInstance();
public static final int DEFAULT_MAX = 100;
public JavaToRuby() {}
public Object randomNumber() throws ScriptException{
return randomNumber(DEFAULT_MAX);
}
public Object randomNumber(int max) throws ScriptException{
ScriptEngine jruby = new ScriptEngineManager().getEngineByName("jruby");
/* jruby.put("message", "hello world"); */
return jruby.eval("puts 'craaaaaaaap!!!!'; rand "+max);
}
}

5
src/try_java_to_ruby.rb Normal file
View File

@@ -0,0 +1,5 @@
include Java
number_generator = com.edgecase.JavaToRuby.new
puts number_generator.random_number
puts "*"*20