Permalink
Browse files

experimenting with more java interop

  • Loading branch information...
1 parent 45523c0 commit 9b97fea75ab3690add797bbfbedc6ee5a3237c8d Marc Peabody committed Sep 28, 2010
View
@@ -10,3 +10,7 @@ task :test do
ruby 'path_to_enlightenment.rb'
end
+task :java do
+ `javac **/*.java`
+end
+
View
@@ -85,5 +85,15 @@ def test_however_most_methods_returning_strings_return_ruby_strings
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.
@@ -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.
@@ -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);
+ }
+}
+
+
View
@@ -0,0 +1,5 @@
+include Java
+
+number_generator = com.edgecase.JavaToRuby.new
+puts number_generator.random_number
+puts "*"*20

0 comments on commit 9b97fea

Please sign in to comment.