Forum: JRuby Proble with closures

F9683b0399bb08a39b1b3e195d9d315f?d=identicon&s=25 Yurij P. (yurij_p)
on 2012-06-20 16:35
Please help. I have jruby script file and need to define closure
callback function that can call script member. How to do this?



latch = CountDownLatch.new(1)

def callback()
   latch.countDown()
end

class Proxy
 def call()
      callback()
 end
end

proxy = Proxy.new()

proxy.call()


when I run it as is I got an exception -> Can't find latch.
F1d37642fdaa1662ff46e4c65731e9ab?d=identicon&s=25 Charles Nutter (headius)
on 2012-06-20 16:43
(Received via mailing list)
This is more a Ruby question, but I'll answer here.

Method definitions are not closures, as you found here. What you want
is a Ruby block:

latch = CountDownLatch.new(1)

callback = ->{ latch.countDown() }

Every call to callback.call will then call latch.countDown.

- Charlie
F9683b0399bb08a39b1b3e195d9d315f?d=identicon&s=25 Yurij P. (yurij_p)
on 2012-06-20 16:56
This syntax as you wrote..

callback = ->{ latch.countDown() }

.. don't want to compile but another one compiled successful

callback = lamda { latch.countDown() }

But it does not work too and I get this error

undefined local variable or method `callback' for #<Proxy:0x13c4c09>
1273e5c266e0c4cc4065fff3a6fe7056?d=identicon&s=25 Anthony Juckel (Guest)
on 2012-06-20 17:08
(Received via mailing list)
On Wed, Jun 20, 2012 at 9:56 AM, Yurij P. <lists@ruby-forum.com> 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.
F9683b0399bb08a39b1b3e195d9d315f?d=identicon&s=25 Yurij P. (yurij_p)
on 2012-06-20 17:16
Many thanks, just 2 minutes ago I tried push callback in intialize
constructor and it works.

Where can I read more about syntax of that statements?

Proxy.class_eval do
  define_method :call, do
    callback.call
  end
end
4bfcb18ca5298df54d8eb410244d004f?d=identicon&s=25 Uwe Kubosch (donv)
on 2012-06-24 09:31
(Received via mailing list)
Hi Yurij!

http://www.google.com/search?q=ruby+1.9+new+lambda+syntax

http://pragdave.blogs.pragprog.com/pragdave/2008/0...
http://railspikes.com/2008/9/8/lambda-in-ruby-1-9
http://www.spritle.com/blogs/2011/05/16/three-new-...


On 2012-06-20, at 17:16, Yurij P. wrote:

> Many thanks, just 2 minutes ago I tried push callback in intialize
> constructor and it works.
>
> Where can I read more about syntax of that statements?
>
> Proxy.class_eval do
>  define_method :call, do
>    callback.call
>  end
> end

--
Uwe Kubosch
http://ruboto.org/
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.