[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

application design advice

snacktime

8/9/2006 9:11:00 PM

I'm using the Eventmachine framework and being fairly new to ruby am
wondering about the best way to structure my code. Being an event
based framework there is a lot of jumping around in the code from one
callback to another.

In particular, most of the methods that I call to set callbacks take a
block. The code I want to run is usually going to be a method within
a class. For instance:

c = Transaction.new
ProtocolHandler.setcallback {|args| c.Process)

This seems to be the easiest way to get the callback to run my method
and pass it the arguments I need. Is there a better way to do this or
am I on the right track?

5 Answers

snacktime

8/9/2006 9:16:00 PM

0

> c = Transaction.new
> ProtocolHandler.setcallback {|args| c.Process)

Oops, that should be..

ProtocolHandler.setcallback {|args| c.Process(args)}

Which brings up another question. How would I write a similar block
to handle multiple arguments where the number of arguments isn't known
when I call setcallback?

Ara.T.Howard

8/9/2006 9:22:00 PM

0

Justin Collins

8/9/2006 9:24:00 PM

0

snacktime wrote:
>> c = Transaction.new
>> ProtocolHandler.setcallback {|args| c.Process)
>
> Oops, that should be..
>
> ProtocolHandler.setcallback {|args| c.Process(args)}
>
> Which brings up another question. How would I write a similar block
> to handle multiple arguments where the number of arguments isn't known
> when I call setcallback?

With the 'splat' operator:

ProtocolHandler.setcallback {|*args| c.Process(*args)}

-Justin

snacktime

8/9/2006 9:34:00 PM

0

>
> ProtocolHandler.setcallback {|*args| c.Process(*args)}

Of course it would be that easy... Thanks!

Daniel Schierbeck

8/10/2006 9:29:00 AM

0

snacktime wrote:
> c = Transaction.new
> ProtocolHandler.setcallback {|args| c.Process)

Just a question: why is #Process capitalized?


Cheers,
Daniel