Altering method lookup

I'm working on a project based on a Smalltalk paper, and a part of it
is to attempt to emulate changes in method lookup in Ruby as done in
Smalltalk. Namely, the paper mentions an alteration of the
no_such_method method in Smalltalk to have it do something before and
after each method execution. In Ruby, I have come as far as doing

   #/usr/bin/env ruby
   
   class Foo
      def bar( baz, qux )
         print baz << " is quite a " << qux << "\n"
      end
      
      def method_missing( aSymbol , *args )
         print "before\n"
         send( :bar, *args )
         print "after\n"
      end
   end

However the results are not quite what I planned. When I use

   quux = Foo.new
   quux.bag( "Dog", "canine" )

(notice that I use an undefined method) I get

   >ruby mmissing.rb
   before
   Dog is quite a canine
   after
   >Exit code: 0

Ok, not quite what I intended, but still useful, and certainly what
it's supposed to do. But when I do

   quux = Foo.new
   quux.bar( "Dog", "canine" )

I get

   >ruby mmissing.rb
   Dog is quite a canine
   >Exit code: 0

Again, after some thinking it does what it is supposed to do (since
method bar is defined, method_missing never gets called). But it is
not what I intend it to do (eventually). What I thought would happen
is that when the object instance first gets the message for bar, bar
(which would be an instance of Method) would be still undefined, so
method_missing would get a message to look up bar, which is why I used
method_missing to start with.
So by now my first questions are:
- How does Ruby do its method lookup? I have spent a moderate time
reading the pickaxe an dlooking up subjects on Google regarding this,
but I couldn't find anything reliable (which doesn't mean that there
isn't - I might have missed a critical part). The closest I got was
some discussions on using method_missing. Pointers/Examples are
greatly appreciated.
- Could I use Object.method or Object.responds_to? to get the results I want?
- If it helps, I eventually would like to create a module that when
implemented does the "before method" and "after method" actions.

Thanks to anyone dedicating time to reading this. Cheers!
  -CWS

- If it helps, I eventually would like to create a module that when
implemented does the "before method" and "after method" actions.

Search for CLOS method combination in ruby-talk

  http://www.ruby-talk.org/

[ruby-talk:38140] http://www.ruby-talk.org/38140

Guy Decoux

I'm working on a project based on a Smalltalk paper, and a part of it
is to attempt to emulate changes in method lookup in Ruby as done in
Smalltalk. Namely, the paper mentions an alteration of the
no_such_method method in Smalltalk to have it do something before and
after each method execution. In Ruby, I have come as far as doing

was'nt it doesNotUnderstand: ?

  #/usr/bin/env ruby
  
  class Foo
     def bar( baz, qux )
        print baz << " is quite a " << qux << "\n"
     end
     
     def method_missing( aSymbol , *args )
        print "before\n"
        send( :bar, *args )

probably
bar *args
would be enough.. Anyway remember to even handle given blocks

Again, after some thinking it does what it is supposed to do (since
method bar is defined, method_missing never gets called). But it is
not what I intend it to do (eventually).

define your own class and delete *all* the methods except __send__
and maybe __id__. Then inherit from that.
see:
http://onestepback.org/index.cgi/Tech/Ruby/BlankSlate.rdoc

also, this may be interesting to you IIUC:
http://www.thekode.net/ruby/techniques/CapturingMethods.html

···

il Sat, 17 Jul 2004 02:23:04 +0900, Claus Spitzer <DocBoobenstein@gmail.com> ha scritto::

I'm not sure I understand this - why would you expect bar to be
undefined at this point?

martin

···

Claus Spitzer <DocBoobenstein@gmail.com> wrote:

not what I intend it to do (eventually). What I thought would happen
is that when the object instance first gets the message for bar, bar
(which would be an instance of Method) would be still undefined, so
method_missing would get a message to look up bar, which is why I used
method_missing to start with.

Thanks to everyone who posted suggestions and links. They are quite
useful indeed, and give me some new ideas.

Regarding the following:

> not what I intend it to do (eventually). What I thought would happen
> is that when the object instance first gets the message for bar, bar
> (which would be an instance of Method) would be still undefined, so
> method_missing would get a message to look up bar, which is why I used
> method_missing to start with.

I'm not sure I understand this - why would you expect bar to be
undefined at this point?

martin

I was under the (probably incorrect) impression (due to other
documents read and a less than optimal memory on the subject) that an
object's instance first doesn't knwo which its methods are. Once they
are found (via, e.g., method_missing) they would be known and all
further references would act on the method, but the first call would
be to method_missing. By now I realize that it is not the way it is
doen in Ruby.

Finally, answering Gabriele Renzi's question: yes, it is
doesNotUnderstand. It had slipped my mind and instead of going back to
the documentation I made up a name. My bad.

Let me again express my gratitude to all who answered.
-CWS

···

On Sat, 17 Jul 2004 04:02:16 +0900, Martin DeMello <martindemello@yahoo.com> wrote:

Claus Spitzer <DocBoobenstein@gmail.com> wrote: