/ Published in: Ruby
Groovy has the as keyword which enables the implementation of a Java interface by using the as keyword, its quite easy to mimic this in JRuby.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
module InvokableHash def as(java_ifc) # Each method will create a key in the hash which points to a block # which is called java_ifc.impl {|name, *args| self[name].call(*args)} end end class Hash include InvokableHash end impl = { :i => 10, :hasNext => proc { impl[:i] > 0 }, :next => proc { impl[:i] -= 1 } } iter = impl.as java.util.Iterator while (iter.hasNext) puts iter.next end
URL: http://blog.headius.com/2007/12/groovy-in-ruby-implement-interface-with.html