Created
June 28, 2011 21:51
-
-
Save 8th-Light-Blog/1052328 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Blog Title: JRuby Helps Us Craft Quality Software | |
Author: Doug Bradbury | |
Date: April 6th, 2010 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//The Java Interface | |
public interface ConfigurationStore | |
{ | |
void put(Map<string> pairs); | |
void put(String key, String value); | |
String get(String name); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#The Ruby Implementation | |
class ConfigurationStore | |
include Java::package.ConfigurationStore | |
def get(key) | |
..... | |
end | |
# Notice how we were able to implement the function | |
# overloading present in the interface by | |
# using a variable arguments in one ruby method. | |
def put(*args) | |
if args.size == 2 | |
.... | |
elsif args.size == 1 | |
..... | |
else | |
raise ArgumentError.new("put() takes a Hash | |
(or a Java Map) or a key and value pair") | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// instantiating a Ruby object in Java | |
RubyInstanceConfig config = new RubyInstanceConfig(); | |
Ruby runtime = JavaEmbedUtils.initialize(new ArrayList(), config); | |
RubyRuntimeAdapter evaler = JavaEmbedUtils.newRuntimeAdapter(); | |
IRubyObject result = evaler.eval(runtime, "require 'configuration_store';" + | |
"config_store = ConfigurationStore.new"); | |
store = (ConfigurationStore) result.toJava(ConfigurationStore.class); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment