July 2011
Intermediate to advanced
400 pages
8h 46m
English
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 )
endIn 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 ...
Read now
Unlock full access