Mirian
29 October 2002 22:05
1
This is a little hard to explain, and I’m not sure if Ruby can
actually do this, but here goes.
I want to write a class with a method, say “method_handler” which
"traps" all calls to undefined methods and executes them somehow. For
example:
class Foo
def method_handler(meth, *args)
# meth is the symbol representing the name of the method
... code to determine what happens ...
end
end
I suspect that it may be possible to do this with an event, but I’m
a little hazy on how that would work.
–Mirian
Hi –
This is a little hard to explain, and I’m not sure if Ruby can
actually do this, but here goes.
I want to write a class with a method, say “method_handler” which
“traps” all calls to undefined methods and executes them somehow. For
example:
class Foo
def method_handler(meth, *args)
# meth is the symbol representing the name of the method
... code to determine what happens ...
end
end
I suspect that it may be possible to do this with an event, but I’m
a little hazy on how that would work.
Your method_handler method is not missing; it’s method_missing
irb(main):007:0> class X
irb(main):008:1> def method_missing(m,*args)
irb(main):009:2> puts “Call to #{m} with args #{args.inspect}!”
irb(main):010:2> end
irb(main):011:1> end
nil
irb(main):012:0> X.new.blah(123,456)
Call to blah with args [123, 456]!
David
···
On Wed, 30 Oct 2002, Mirian Crzig Lennox wrote:
–
David Alan Black
home: dblack@candle.superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav
Method_missing will be executed on a class when you call a method that
is missing on that class.
Example:
def method_missing(symbol, *arguments, &block)
#do something
end
···
-----Original Message-----
From: Mirian Crzig Lennox [mailto:mirian@cosmic.com ]
Sent: Tuesday, October 29, 2002 5:05 PM
To: ruby-talk ML
Subject: Dynamically executed methods in Ruby?
This is a little hard to explain, and I’m not sure if Ruby can actually
do this, but here goes.
I want to write a class with a method, say “method_handler” which
"traps" all calls to undefined methods and executes them somehow. For
example:
class Foo
def method_handler(meth, *args)
# meth is the symbol representing the name of the method
... code to determine what happens ...
end
end
I suspect that it may be possible to do this with an event, but I’m a
little hazy on how that would work.
–Mirian
Mirian Crzig Lennox wrote:
I want to write a class with a method, say “method_handler” which
“traps” all calls to undefined methods and executes them somehow.
Just define the method_missing method in your class.
http://www.rubycentral.com/book/ref_c_object.html#Object.method_missing
···
–
([ Kent Dahl ]/)_ ~ [ http://www.stud.ntnu.no/~kentda/ ] /~
))_student /(( _d L b _/ NTNU - graduate engineering - 5. year )
( __õ|õ // ) )Industrial economics and technological management(
_ /ö ____/ (_engineering.discipline =Computer::Technology )
Try this instead:
class Foo
def method_missing(meth, *args)
# meth is the symbol representing the name of the method
… code to determine what happens …
end
end
Once again Matz proves to be good in reading programmer’ mind.
···
On 2002-10-30 07:05:10 +0900, Mirian Crzig Lennox wrote:
I want to write a class with a method, say “method_handler” which
“traps” all calls to undefined methods and executes them somehow. For
example:
class Foo
def method_handler(meth, *args)
# meth is the symbol representing the name of the method
... code to determine what happens ...
end
end
–
A theologian is like a blind man in a dark room searching for a black cat
which isn’t there - and finding it!
Mirian
29 October 2002 23:13
6
This is wonderful! Thanks, Matz!!
And thanks everyone who responded,
–Mirian
···
On Wed, 30 Oct 2002 07:41:20 +0900, Florian Frank flori@nixe.ping.de wrote:
Try this instead:
class Foo
def method_missing(meth, *args)
meth is the symbol representing the name of the method
... code to determine what happens ...
end
end
Once again Matz proves to be good in reading programmer’ mind.