[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Dispatching a function based on a message

Edgardo Hames

2/5/2005 5:08:00 PM

I'm back writing my small network app (IM client) and I need to
dispatch functions based on the messages received from the server. For
example, if my app gets a string like

MSG msg_number som_other_text

it should call the function process_MSG and pass some_other_text as an argument.

How can I do that? I was thinking of a big case statement, but I'm not
sure that's the Ruby Way. Probably a hash with MSGs as keys and
process_MSGs as values?

Thanks for your ideas,
Ed
--
Alcohol is the anesthesia by which we endure the operation of life.
-- George Bernard Shaw


2 Answers

mark sparshatt

2/5/2005 5:30:00 PM

0

--- Edgardo Hames <ehames@gmail.com> wrote:
> I'm back writing my small network app (IM client)
> and I need to
> dispatch functions based on the messages received
> from the server. For
> example, if my app gets a string like
>
> MSG msg_number som_other_text
>
> it should call the function process_MSG and pass
> some_other_text as an argument.
>
> How can I do that? I was thinking of a big case
> statement, but I'm not
> sure that's the Ruby Way. Probably a hash with MSGs
> as keys and
> process_MSGs as values?
>

You could try using the send method

class MyClass
def process_MSG(val)
#do something
end
end

if you've split the message up into the variables msg,
msg_number, other_text then you'd just need to do

processor = MyClass.new
processor.send("process_#{msg), other_text)

HTH

--
Mark Sparshatt








___________________________________________________________
ALL-NEW Yahoo! Messenger - all new features - even more fun! http://uk.messenger...


Robert Klemme

2/6/2005 6:38:00 PM

0


"Mark Sparshatt" <msparshatt@yahoo.co.uk> schrieb im Newsbeitrag
news:20050205172919.63235.qmail@web53106.mail.yahoo.com...
> --- Edgardo Hames <ehames@gmail.com> wrote:
>> I'm back writing my small network app (IM client)
>> and I need to
>> dispatch functions based on the messages received
>> from the server. For
>> example, if my app gets a string like
>>
>> MSG msg_number som_other_text
>>
>> it should call the function process_MSG and pass
>> some_other_text as an argument.
>>
>> How can I do that? I was thinking of a big case
>> statement, but I'm not
>> sure that's the Ruby Way. Probably a hash with MSGs
>> as keys and
>> process_MSGs as values?
>>
>
> You could try using the send method
>
> class MyClass
> def process_MSG(val)
> #do something
> end
> end
>
> if you've split the message up into the variables msg,
> msg_number, other_text then you'd just need to do
>
> processor = MyClass.new
> processor.send("process_#{msg), other_text)
>
> HTH

We can even wrap that:

class MyClass
def process_MSG(val) ... end

def receive(line)
if /\A(\S+)\s+\d+\s+(.*)\Z/ =~ line
send($1,$2)
end
end
end

Kind regards

robert