Example

hello,

I’m goint to write a ruby extension for an extern hardware tool.
When my extension receives data (i.e.) from the serial
interface (com1), a function in the ruby code should be
``executed´´

So I’m searching now for a simple example which show how
to implement callback functions.

Does anybody known a good link for this?

daniel

I don’t know about good links… but if I were doing this, I would run the
data receiver as a separate thread. It then simply has to receive messages,
and when it has a complete message that it wants to do something with, it
just does. This might mean putting it in a queue which the main thread picks
from. You can use mutexes and condition variables to make the queue
thread-safe, and to allow the main thread to sleep until a message arrives.
I’d recommend you start with the Pickaxe chapter on threading, and then
there are a number of threading examples on the Wiki
(http://www.rubygarden.org/ruby)

I seem to remember seeing a module for handling serial ports on RAA
somewhere.

Cheers,

Brian.

···

On Thu, Apr 17, 2003 at 05:54:49AM +0900, daniel wrote:

I’m goint to write a ruby extension for an extern hardware tool.
When my extension receives data (i.e.) from the serial
interface (com1), a function in the ruby code should be
``executed??

So I’m searching now for a simple example which show how
to implement callback functions.

Does anybody known a good link for this?

“Brian Candler” B.Candler@pobox.com wrote in message
[snip]

Does anybody known a good link for this?

I don’t know about good links… but if I were doing this, I would run the
data receiver as a separate thread. It then simply has to receive
messages,
and when it has a complete message that it wants to do something with, it
just does. This might mean putting it in a queue which the main thread
picks
from. You can use mutexes and condition variables to make the queue
thread-safe, and to allow the main thread to sleep until a message
arrives.

And if you want to receive data from multiple distributed data points, you
may
want to build a distributed Ruby architecture using dRuby: makes it very
simple,
yet elegant and powerful.See http://raa.ruby-lang.org/list.rhtml?name=druby

HTH,
– shanko

Brian Candler wrote:

···

On Thu, Apr 17, 2003 at 05:54:49AM +0900, daniel wrote:

[…]

I don’t know about good links… but if I were doing this, I would run the
data receiver as a separate thread.
[…]

I known this all (I’m using a thread now). I’m only searching for an
implementation of a callback fn…

I’m checking tk now (it’s ``bind´´ functions).

daniel

You could mean several things by a ‘callback function’:

  • you can simply past a Proc object. It keeps all the bindings to the
    environment it was created in, and can be called at any time by
    .call(args)

  • you might be thinking of a Binding object, which is as far as I understand
    just the variable binding part without the actual proc to run :slight_smile:

    http://www.rubycentral.com/book/ref_c_binding.html

  • you might be thinking of continuations

But the nearest thing to what I understand by a ‘callback function’ is
simply a Proc that you pass to the person you want to call you back.

Regards,

Brian.

···

On Thu, Apr 17, 2003 at 06:20:34PM +0900, daniel wrote:

I known this all (I’m using a thread now). I’m only searching for an
implementation of a callback fn…

I’m checking tk now (it’s ``bind?? functions).