public Method

Module.undef_method(...)

undef_method(symbol)     self

Prevents the current class from responding to calls to the named method. Contrast this with remove_method, which deletes the method from the particular class; Ruby will still search superclasses and mixed-in modules for a possible receiver.

class Parent
  def hello
    puts "In parent"
  end
end
class Child < Parent
  def hello
    puts "In child"
  end
end

c = Child.new
c.hello

class Child
  remove_method :hello  # remove from child, still in parent
end
c.hello

class Child
  undef_method :hello   # prevent any calls to 'hello'
end
c.hello

produces:

In child
In parent
prog.rb:23: undefined method `hello' for #<child:0x401b3bb4> (NoMethodError)</child:0x401b3bb4>

Source Code

/*
*  call-seq:
*     undef_method(symbol)    => self
*  
*  Prevents the current class from responding to calls to the named
*  method. Contrast this with <code>remove_method</code>, which deletes
*  the method from the particular class; Ruby will still search
*  superclasses and mixed-in modules for a possible receiver.
*     
*     class Parent
*       def hello
*         puts "In parent"
*       end
*     end
*     class Child < Parent
*       def hello
*         puts "In child"
*       end
*     end
*     
*     
*     c = Child.new
*     c.hello
*     
*     
*     class Child
*       remove_method :hello  # remove from child, still in parent
*     end
*     c.hello
*     
*     
*     class Child
*       undef_method :hello   # prevent any calls to 'hello'
*     end
*     c.hello
*     
*  <em>produces:</em>
*     
*     In child
*     In parent
*     prog.rb:23: undefined method `hello' for #<Child:0x401b3bb4> (NoMethodError)
*/

static VALUE
rb_mod_undef_method(argc, argv, mod)
   int argc;
   VALUE *argv;
   VALUE mod;
{
   int i;

   for (i=0; i<argc; i++) {
       rb_undef(mod, rb_to_id(argv[i]));
   }
   return mod;
}
Comments

Have your say
Please use Textile formatting (click here for a cheat sheet). Use <code/> and <pre/> for code samples.
Click here to login with OpenID to to post comments.