mirror of
https://github.com/edgecase/ruby_koans.git
synced 2026-04-13 06:43:20 -04:00
experimenting with more java interop
This commit is contained in:
@@ -10,3 +10,7 @@ task :test do
|
||||
ruby 'path_to_enlightenment.rb'
|
||||
end
|
||||
|
||||
task :java do
|
||||
`javac **/*.java`
|
||||
end
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
BIN
src/com/edgecase/JavaStuff.class
Normal file
BIN
src/com/edgecase/JavaStuff.class
Normal file
Binary file not shown.
18
src/com/edgecase/JavaStuff.java
Normal file
18
src/com/edgecase/JavaStuff.java
Normal 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;
|
||||
}
|
||||
}
|
||||
BIN
src/com/edgecase/JavaToRuby.class
Normal file
BIN
src/com/edgecase/JavaToRuby.class
Normal file
Binary file not shown.
28
src/com/edgecase/JavaToRuby.java
Normal file
28
src/com/edgecase/JavaToRuby.java
Normal 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
5
src/try_java_to_ruby.rb
Normal file
@@ -0,0 +1,5 @@
|
||||
include Java
|
||||
|
||||
number_generator = com.edgecase.JavaToRuby.new
|
||||
puts number_generator.random_number
|
||||
puts "*"*20
|
||||
Reference in New Issue
Block a user