method_missing

D

Dirk Meijer

------=_Part_59310_20217798.1131562584836
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

hi everyone,
i've got a small problem and can't figure out how to solve it.
in my program, you can enter commands, and then it checks if those commands
exist within a module..
if the command does not exist, it should tell the user that, this gets me t=
o
this:
def method_missing(method,*trash)
print "Command #{method} not found!\n"
end
but when the user enters a command with an argument (only one argument is
read), it prints the following:
Command <argument> not found!
Command <method> not found!
which doesn't look very nice.. how would i solve this?
greetings, Dirk.

------=_Part_59310_20217798.1131562584836--
 
J

Jeffrey Dik

What about not using method_missing and catching the NoMethodError
exception in the appropriate place?

Hope that helps,
Jeff
 
M

Mark Hubbart

hi everyone,
i've got a small problem and can't figure out how to solve it.
in my program, you can enter commands, and then it checks if those comman= ds
exist within a module..
if the command does not exist, it should tell the user that, this gets me= to
this:
def method_missing(method,*trash)
print "Command #{method} not found!\n"
end
but when the user enters a command with an argument (only one argument i= s
read), it prints the following:
Command <argument> not found!
Command <method> not found!
which doesn't look very nice.. how would i solve this?

It might help if you post some more of the code, especially that
related to taking the user's input and calling the methods.

My guess is that it lies in that area; it looks like maybe you are
accidentally "send"ing the argument as a method call.

cheers,
Mark
 
D

Dirk Meijer

------=_Part_59806_25223082.1131563892587
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

hi,
thanks, the NoMethodError catching works :)
and Mark, the argument has to be sent, otherwise the existing methods
wouldn't get the arguments either (it looks like this:
eval("Module::#{command}(#{argument})")
but the problem is solved then :)
greetings, Dirk.

2005/11/9 said:
It might help if you post some more of the code, especially that
related to taking the user's input and calling the methods.

My guess is that it lies in that area; it looks like maybe you are
accidentally "send"ing the argument as a method call.

cheers,
Mark

------=_Part_59806_25223082.1131563892587--
 
M

Mark Hubbart

hi,
thanks, the NoMethodError catching works :)
and Mark, the argument has to be sent, otherwise the existing methods
wouldn't get the arguments either (it looks like this:
eval("Module::#{command}(#{argument})")

ahh :) that was it. Where command =3D "foo" and argument =3D "bar":

eval("Module::#{command}(#{argument})")

evaluates the code:

Module::foo(bar)

... where bar is not a string, it's a method call :)

Instead of the evil eval, you might try:

Module.__send__(command, argument)

This will work in the way you expect.

Also, I agree that catching the error is the best way to deal with the
situation. Though method_missing is really cool :)

cheers,
Mark
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top