On Wed, Jun 20, 2012 at 9:56 AM, Yurij P. [email protected] wrote:
But it does not work too and I get this error
undefined local variable or method `callback’ for #Proxy:0x13c4c09
That’s the same problem, just pushed to a new reference. Without
questioning why you’re wanting your methods to be closures, here’s a
version that would work:
require ‘java’
import ‘java.util.concurrent.CountDownLatch’
latch = CountDownLatch.new(1)
callback = lambda { latch.countDown }
class Proxy
def initialize(callback)
@callback = callback
end
def call()
@callback.call
end
end
proxy = Proxy.new(callback)
proxy.call()
Or if you REALLY want those methods to be closures, define them with
blocks, rather than with def.
require ‘java’
import ‘java.util.concurrent.CountDownLatch’
latch = CountDownLatch.new(1)
callback = lambda { latch.countDown }
class Proxy
end
Proxy.class_eval do
define_method :call, do
callback.call
end
end
proxy = Proxy.new()
proxy.call()
But, again, before you start doing trickery like that, consider why
you’re going down this road.