Remove/undef a class method

后端 未结 6 1349
滥情空心
滥情空心 2020-12-13 03:59

You can dynamically define a class method for a class like so:

class Foo
end

bar = %q{def bar() \"bar!\" end}
Foo.instance_eval(bar)

But h

相关标签:
6条回答
  • 2020-12-13 04:06

    Object.send(:remove_const, :Foo)

    0 讨论(0)
  • 2020-12-13 04:12

    I guess I can't comment on Adrian's answer because I don't have enough cred, but his answer helped me.

    What I found: undef seems to completely remove the method from existence, while remove_method removes it from that class, but it will still be defined on superclasses or other modules that have been extened on this class, etc.

    0 讨论(0)
  • 2020-12-13 04:26

    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 - main.rb:15:in ': 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
    

    Result - $ruby main.rb

    x from A class

    0 讨论(0)
  • 2020-12-13 04:28

    This also works for me (not sure if there are differences between undef and remove_method):

    class Foo
    end
    
    Foo.instance_eval do
      def color
        "green"
      end
    end
    
    Foo.color # => "green"
    
    Foo.instance_eval { undef :color }
    
    Foo.color # => NoMethodError: undefined method `color' for Foo:Class
    
    0 讨论(0)
  • 2020-12-13 04:28

    If you would like to remove method with name what calculate dinamically, you should use eigenclasses like:

    class Foo
      def self.bar
        puts "bar"
      end
    end
    
    name_of_method_to_remove = :bar
    eigenclass = class << Foo; self; end
    eigenclass.class_eval do
      remove_method name_of_method_to_remove
    end
    

    this way is better than others answers, becouse here i used class_eval with block. As you now block see current namespace, so you could use your variables to remove methods dinamically

    0 讨论(0)
  • 2020-12-13 04:30
    class Foo
      def self.bar
        puts "bar"
      end
    end
    
    Foo.bar    # => bar
    
    class <<Foo
      undef_method :bar
    end
    # or
    class Foo
      singleton_class.undef_method :bar
    end
    
    Foo.bar    # => undefined method `bar' for Foo:Class (NoMethodError)
    

    When you define a class method like Foo.bar, Ruby puts it Foo's singleton class. Ruby can't put it in Foo, because then it would be an instance method. Ruby creates Foo's singleton class, sets the superclass of the singleton class to Foo's superclass, and then sets Foo's superclass to the singleton class:

    Foo -------------> Foo(singleton class) -------------> Object
            super      def bar             super
    

    There are a few ways to access the singleton class:

    • class <<Foo,
    • Foo.singleton_class,
    • class Foo; class << self which is commonly use to define class methods.

    Note that we used undef_method, we could have used remove_method. The former prevents any call to the method, and the latter only removes the current method, having a fallback to the super method if existing. See Module#undef_method for more information.

    0 讨论(0)
提交回复
热议问题