Skip to content

Instantly share code, notes, and snippets.

@headius

headius/.java Secret

Created March 24, 2022 15:09
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 headius/f9d6108d89c97bb7b2009254e3f79409 to your computer and use it in GitHub Desktop.
Save headius/f9d6108d89c97bb7b2009254e3f79409 to your computer and use it in GitHub Desktop.
public class RespondToJavaService implements BasicLibraryService {
@Override
public boolean basicLoad(Ruby ruby) throws IOException {
ruby.defineModule("Foo").defineClassUnder("Bar", ruby.getObject(), new ObjectAllocator() {
@Override
public IRubyObject allocate(Ruby runtime, RubyClass klazz) {
return new RespondTo(runtime, klazz);
}
}).defineAnnotatedMethods(RespondTo.class);
return true;
}
@JRubyClass(name = "RespondTo")
public static class RespondTo extends RubyObject {
public RespondTo(Ruby runtime, RubyClass klazz) {
super(runtime, klazz);
}
@JRubyMethod(name = "respond_to?", required = 1, optional = 1)
public static IRubyObject respondTo(ThreadContext context, IRubyObject self, IRubyObject[] args) {
System.err.println("Custom repond_to? got called!");
// below line requires `frame = true` to set up a call frame for `super`
// return Helpers.invokeSuper(context, this, metaClass, "respond_to?", args, Block.NULL_BLOCK);
// instead you can just call the default impl directly
return self.respondTo(context, args);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment