Mauricio Fernández alias batsman [mailto:batsman.geo@yahoo.com] brilliantly
replied:
However, I was stuck on “how does one know what methods
were passed”.
I want to do this so I can say (especially to my son), “See son, it
(the dog) got what you asked for. Next, we will now let it
do what we
asked…”… I thought this was possible since there is a
method_missing() method wc knows the method passed
(-unfortunately it
is missing :-)…
You could simulate that with some meta-programming magic
involving alias_method and method_added, but I wouldn’t show
that code to your son 
batsman@tux-chan:/tmp$ expand -t2 e.rb
keep this secret 
module MethodCalledMagic
def method_added(id)
@level ||= 0
return if @level == 1
@level += 1
alias_method “real#{id}”, id
module_eval <<-EOF
def #{id}(*a,&b)
puts “You called #{self.inspect}##{id}”
real#{id}(*a,&b)
end
EOF
@level -= 1
end
end
this is what your son sees
class Dog
extend MethodCalledMagic
end
d = Dog.new
class Dog
def foo; puts “the dog does foo” end
def bar; puts “the dog does bar” end
end
d.foo
d.bar
batsman@tux-chan:/tmp$ ruby e.rb
You called Dog#foo
the dog does foo
You called Dog#bar
the dog does bar
Wow. That was close. However it does not do the “unknown methods”.
Consider it if I undef bar…
ruby e.rb
You called Dog#foo
the dog does foo
a2.rb:30: undefined method `bar’ for #Dog:0x27769a0 (NoMethodError)
I would prefer the output to be:
You called Dog#foo
the dog does foo
You called Dog#bar
a2.rb:30: undefined method `bar’ for #Dog:0x27769a0 (NoMethodError)
Is this possible?
kind regards -botp
batsman@tux-chan:/tmp$ ruby e.rb
You called Dog#foo
the dog does foo
You called Dog#bar
the dog does bar
You called Dog#bar
e.rb:21:in method_missing': undefined method bar’ for #Dog:0x401c8034 (NoMethodError)
from e.rb:19:in `method_missing’
from e.rb:42
batsman@tux-chan:/tmp$ expand -t2 e.rb
keep this secret 
module MethodCalledMagic
def method_added(id)
@level ||= 0
return if @level == 1
@level += 1
alias_method “real#{id}”, id
module_eval <<-EOF
def #{id}(*a,&b)
puts “You called #{self.inspect}##{id}”
real#{id}(*a,&b)
end
EOF
@level -= 1
end
def self.extend_object(obj)
obj.send :define_method, :method_missing do |id,*a|
puts “You called #{self.class}##{id}”
super id
end
super
end
end
this is what your son sees
class Dog
extend MethodCalledMagic #
end
d = Dog.new
class Dog
def foo; puts “the dog does foo” end
def bar; puts “the dog does bar” end
end
d.foo
d.bar
class Dog; undef_method :bar end
d.bar
You could also redefine Module#undef_method.
···
On Mon, Jan 26, 2004 at 12:02:11PM +0900, “Peña, Botp” wrote:
batsman@tux-chan:/tmp$ ruby e.rb
You called Dog#foo
the dog does foo
You called Dog#bar
the dog does bar
Wow. That was close. However it does not do the “unknown methods”.
Consider it if I undef bar…
ruby e.rb
You called Dog#foo
the dog does foo
a2.rb:30: undefined method `bar’ for #Dog:0x27769a0 (NoMethodError)
I would prefer the output to be:
You called Dog#foo
the dog does foo
You called Dog#bar
a2.rb:30: undefined method `bar’ for #Dog:0x27769a0 (NoMethodError)
Is this possible?
–
_ _
__ __ | | ___ _ __ ___ __ _ _ __
'_ \ / | __/ __| '_ _ \ / ` | ’ \
) | (| | |__ \ | | | | | (| | | | |
.__/ _,|_|/| || ||_,|| |_|
Running Debian GNU/Linux Sid (unstable)
batsman dot geo at yahoo dot com
Actually, typing random strings in the Finder does the equivalent of
filename completion.
– Discussion on file completion vs. the Mac Finder
keep this secret 
module MethodCalledMagic
def method_added(id)
@level ||= 0
return if @level == 1
@level += 1
alias_method “real#{id}”, id
module_eval <<-EOF
def #{id}(*a,&b)
puts “You called #{self.inspect}##{id}”
real#{id}(*a,&b)
end
EOF
@level -= 1
end
def self.extend_object(obj)
obj.send :define_method, :method_missing do |id,*a|
puts “You called #{self.class}##{id}”
super id
end
super
end
end
Promise us you will never show this code to anyone who is considering
ruby. They will run away screaming halfway through the first method.
···
–
Zachary P. Landau kapheine@hypa.net
GPG: gpg --recv-key 0x24E5AD99 | http://kapheine.hypa.net/kapheine.asc
keep this secret 
module MethodCalledMagic
[…]
end
Promise us you will never show this code to anyone who is considering
ruby. They will run away screaming halfway through the first method.
batsman@tux-chan:/tmp$ diff -u e.rb.old e.rb
— e.rb.old 2004-01-27 14:38:08.000000000 +0100
+++ e.rb 2004-01-27 14:41:37.000000000 +0100
@@ -1,5 +1,6 @@
-# keep this secret 
+### !!! TOP SECRET !!!
+# NOT TO BE DISCLOSED TO RUBY NEWBIES UNDER ANY CIRCUMSTANCE
module MethodCalledMagic
def method_added(id)
@level ||= 0
@@ -24,6 +25,8 @@
end
end
+### end of top secret section
···
On Tue, Jan 27, 2004 at 04:40:59PM +0900, Zachary P. Landau wrote:
+
this is what your son sees
class Dog
extend MethodCalledMagic #

–
_ _
__ __ | | ___ _ __ ___ __ _ _ __
'_ \ / | __/ __| '_ _ \ / ` | ’ \
) | (| | |__ \ | | | | | (| | | | |
.__/ _,|_|/| || ||_,|| |_|
Running Debian GNU/Linux Sid (unstable)
batsman dot geo at yahoo dot com
It’s easy to get on the internet and forget you have a life
– Topic on #LinuxGER