Very cool stuff!

-Bill

On 6/30/07, Nick Sieger <[EMAIL PROTECTED]> wrote:

I just committed a patch I had lingering in JRUBY-991, plus some
enhancements.  We can back out the change if it causes problems or
proves to be too controversial.  The basic theme is auto-coercion of
vanilla Ruby objects to Java interface proxies when the ruby object
ends up in the same argument position as a Java interface type when
passed to a Java method invocation.  Again, the example given in the
issue:

---
include Java

filter = Object.new
def filter.accept(file, str)
  !!(str =~ /jar/)
end

file = java.io.File.new("lib")
file.list(filter).each {|f| puts f}
---

I took this a step further and applied some further magic for procs,
so that if a Ruby proc gets passed to a Java method, it will have a
custom method_missing injected to allow the proc to be called whenever
an interface method is invoked.  This shortens the example above to

---
include Java

file = java.io.File.new("lib")
file.list(proc {|file,str| !!(str =~ /jar/) }).each {|f| puts f}
---

Furthermore, I added code to allow blocks to be passed to Java method
invocations.  They will get converted to procs and will behave similar
to the previous example.  So, ever so slightly shorter:

---
include Java

file = java.io.File.new("lib")
file.list {|file,str| !!(str =~ /jar/) }.each {|f| puts f}
---

So, the holy grail of Ruby closure integration with Java method
invocation is nearly achieved.  Let me know what you think.

/Nick

---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email


Reply via email to