Removing Methods
Recall you created a new method earlier (dynamic.rb) using send
to call define_method
and passed to it the name, m
, of the method to be created plus a block, &block
, containing the code of the new method:
dynamic.rb
def addMethod( m, &block ) self.class.send( :define_method, m , &block ) end
In addition to creating new methods, sometimes you may want to remove existing methods. You can do this using remove_method
within the scope of a given class. This removes the method specified by a symbol from a specific class:
rem_methods1.rb
puts( "hello".reverse ) #=> olleh class String remove_method( :reverse ) end puts( "hello".reverse ) #=> undefined method error!
If a method with the same name is defined for an ancestor of that class, the ...
Get The Book of Ruby now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.