>>>>> "S" == Stefan Mueller <flcl / gmx.net> writes:

S> 	The following (pickaxe:348) does work,
S> 	but not the way I would like it to, just like the pickaxe original.

 Write it like this, you'll understand what it do

--------> 
S> module Mod
S> 	alias_method :origExit, :exit

S> 	def exit(code=0)
S> 		puts "exit code #{code}"
S> 		origExit(code)
S> 	end
S> end

S> class Foo
S> 	def exit(code=0)
S> 		puts code
                
                super

S> 	end
S> end

S> class Foo
S> 	include Mod
S> end


S> f = Foo.new
S> f.exit(1)		# => 1, but I want "exit code 1"

 it has found Foo#exit before Mod#exit

 If you add #super in Foo#exit it will call Mod#exit

S> <---------	


S> 	I don't understand, why "exit" works, and "foo" not.
S> 	Doing the following does not show "exit"

irb> module Mod
irb> puts methods
irb> end

 Write it

    module Mod
       puts private_methods
    end


 #exit is a private method for Mod

 You can create an alias only for an existent method. Now for your example,
 #foo is defined for Foo and not for Mod this is why it give you an
 error. You can try something like this

pigeon% cat b.rb
#!/usr/bin/ruby
module Mod
   def self.append_features(kl)
      super
      if kl.method_defined?(:foo) && !kl.method_defined?(:origFoo)
         kl.class_eval <<-EOT
            alias_method :origFoo, :foo
            def foo(code=0)
               puts 'foo code ' + code.to_s
               origFoo(code)
            end
         EOT
      end
   end
end

class Foo
   def foo(code=0)
     puts code
   end
end

class Foo
   include Mod
end

f = Foo.new
f.foo(1)        # =>  "foo code 1", I wish
pigeon% 

pigeon% b.rb
foo code 1
1
pigeon% 


 When ruby make the include, it will call ::append_features.

 Verify if the method #foo is defined in the class, and if yes redefine
 it. 


Guy Decoux