Skip to content

Instantly share code, notes, and snippets.

@8th-Light-Blog
Created June 28, 2011 21:51
Show Gist options
  • Save 8th-Light-Blog/1052328 to your computer and use it in GitHub Desktop.
Save 8th-Light-Blog/1052328 to your computer and use it in GitHub Desktop.
Blog Title: JRuby Helps Us Craft Quality Software
Author: Doug Bradbury
Date: April 6th, 2010
//The Java Interface
public interface ConfigurationStore
{
void put(Map<string> pairs);
void put(String key, String value);
String get(String name);
}
#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
// 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