Difference between remove_method and undef_method

You can remove a method in two easy ways. The drastic
Module#undef_method( ) 
removes all methods, including the inherited ones. The kinder
Module#remove_method( ) 
removes the method from the receiver, but it leaves inherited methods alone.
See below 2 simple example -

Example 1 using undef_method
class A 
    def x
        puts "x from A class"
    end
end

class B < A
    def x
        puts "x from B Class"
    end
    undef_method :x
end

obj = B.new
obj.x
result: undefined methodx' for # (NoMethodError)

Example 2 using remove_method
class A 
    def x
        puts "x from A class"
    end
end

class B < A
    def x
        puts "x from B Class"
    end
    remove_method :x
end

obj = B.new
obj.x
x from A class

Comments

Popular posts from this blog

Installing Wowza Streaming Engine on ubuntu

Highcharts with grouped categories

Completely Uninstall and Install Rubymine on ubuntu