Skip to content

Instantly share code, notes, and snippets.

@qmx
Created April 5, 2011 19:24
Show Gist options
  • Save qmx/904322 to your computer and use it in GitHub Desktop.
Save qmx/904322 to your computer and use it in GitHub Desktop.
@JRubyClass(name = "Date")
public class RubyDate extends RubyObject {
public static class RubyDateSingleton {
@JRubyMethod(name = "jd_to_civil", required = 1, optional = 1, visibility = Visibility.PRIVATE)
public IRubyObject jd_to_civil(ThreadContext ctx, IRubyObject[] args) {
System.out.println("LOOOOL");
return ctx.nil;
}
}
private static final ObjectAllocator DATE_ALLOCATOR = new ObjectAllocator() {
public IRubyObject allocate(Ruby runtime, RubyClass klass) {
return new RubyDate(runtime, klass);
}
};
public static RubyClass createDate(Ruby runtime) {
RubyClass result = runtime.defineClass("Date", runtime.getObject(), DATE_ALLOCATOR);
result.defineAnnotatedMethods(RubyDate.class);
result.defineAnnotatedConstants(RubyDate.class);
RubyModule module = RubyModule.newModule(runtime);
module.defineAnnotatedMethods(RubyDateSingleton.class);
result.include(new IRubyObject[] { module });
result.extend(new IRubyObject[] { module });
result.getSingletonClass().include(new IRubyObject[] { module });
result.getSingletonClass().extend(new IRubyObject[] { module });
return result;
}
public RubyDate(Ruby runtime, RubyClass klass) {
super(runtime, klass);
}
> require 'date'
> Date.new
NoMethodError: undefined method `jd_to_civil' for Date:Class
from /Volumes/v2/git/jruby/lib/ruby/1.9/date.rb:625:in `_valid_civil?'
from /Volumes/v2/git/jruby/lib/ruby/1.9/date.rb:808:in `civil'
from (irb):2:in `evaluate'
from org/jruby/RubyKernel.java:1087:in `eval'
from org/jruby/RubyKernel.java:1412:in `loop'
from org/jruby/RubyKernel.java:1199:in `catch'
from org/jruby/RubyKernel.java:1199:in `catch'
from /Volumes/v2/git/jruby/bin/jirb:13:in `(root)'
t = Module.new do
private
def jd_to_civil(a,b)
...
end
end
Date.extend(t)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment