Method_missing

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 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 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.

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

Hope that helps,
Jeff

···

On Thu, Nov 10, 2005 at 03:56:27AM +0900, Dirk Meijer wrote:

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 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 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.

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 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 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?

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

···

On 11/9/05, Dirk Meijer <hawkman.gelooft@gmail.com> wrote:

greetings, Dirk.

hi,
thanks, the NoMethodError catching works :slight_smile:
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 :slight_smile:
greetings, Dirk.

···

2005/11/9, Mark Hubbart <discordantus@gmail.com>:

On 11/9/05, Dirk Meijer <hawkman.gelooft@gmail.com> wrote:
> 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 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
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?

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

> greetings, Dirk.
>
>

hi,
thanks, the NoMethodError catching works :slight_smile:
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 :slight_smile: that was it. Where command = "foo" and argument = "bar":

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

evaluates the code:

  Module::foo(bar)

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

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 :slight_smile:

cheers,
Mark

···

On 11/9/05, Dirk Meijer <hawkman.gelooft@gmail.com> wrote:

but the problem is solved then :slight_smile:
greetings, Dirk.

2005/11/9, Mark Hubbart <discordantus@gmail.com>:
>
> On 11/9/05, Dirk Meijer <hawkman.gelooft@gmail.com> wrote:
> > 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 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
> 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?
>
> 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
>
> > greetings, Dirk.
> >
> >
>
>