On Mon, Jan 26, 2004 at 12:02:11PM +0900, "Peņá, 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?

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

-- 
 _           _                             
| |__   __ _| |_ ___ _ __ ___   __ _ _ __  
| '_ \ / _` | __/ __| '_ ` _ \ / _` | '_ \ 
| |_) | (_| | |_\__ \ | | | | | (_| | | | |
|_.__/ \__,_|\__|___/_| |_| |_|\__,_|_| |_|
	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