Last active
December 16, 2015 13:39
-
-
Save durran/5443169 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
module Ext | |
module Int | |
def to_json | |
# Do some encoding here. | |
end | |
end | |
end | |
::Integer.send(:include, Ext::Int) |
This file contains hidden or 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
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