Return to Snippet

Revision: 17446
at September 4, 2009 18:21 by narkisr


Initial Code
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

Initial URL
http://blog.headius.com/2007/12/groovy-in-ruby-implement-interface-with.html

Initial Description
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.

Initial Title
Groovy Map as Interface method in JRuby

Initial Tags
ruby, groovy

Initial Language
Ruby