Skip to content

Instantly share code, notes, and snippets.

@durran
Last active December 16, 2015 13:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save durran/5443169 to your computer and use it in GitHub Desktop.
Save durran/5443169 to your computer and use it in GitHub Desktop.
module Ext
module Int
def to_json
# Do some encoding here.
end
end
end
::Integer.send(:include, Ext::Int)
package org.bson;
import java.io.IOException;
import org.jruby.Ruby;
import org.jruby.RubyModule;
import org.jruby.RubyString;
import org.jruby.anno.JRubyMethod;
import org.jruby.runtime.builtin.IRubyObject;
import org.jruby.runtime.load.BasicLibraryService;
public class NativeService implements BasicLibraryService {
private final String EXT = "EXT".intern();
public boolean basicLoad(Ruby runtime) throws IOException {
RubyModule ext = runtime.fastGetModule(EXT);
new IntegerExtension(runtime, ext).redefine();
return true;
}
public class IntegerExtension {
private final String INTEGER = "Integer".intern();
private final Ruby runtime;
private final RubyModule integer;
private IntegerExtension(final Ruby runtime, final RubyModule ext) {
this.runtime = runtime;
this.integer = ext.defineOrGetModuleUnder(INTEGER);
}
public void redefine() {
// This doesn't work at runtime since:
// Java::JavaLang::ClassCastException:
// org.jruby.RubyFixnum cannot be cast to org.bson.NativeService$IntegerExtension
integer.defineAnnotatedMethods(IntegerExtension.class);
}
@JRubyMethod(name = "to_json") public IRubyObject toJson() {
// Do some encoding here.
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment