问题描述:

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 how do you do the opposite: remove/undefine a class method? I suspect Module's remove_method and undef_method methods might be able to be used for this purpose, but all of the examples I've seen after Googling for hours have been for removing/undefining instance methods, not class methods. Or perhaps there's a syntax you can pass to instance_eval to do this as well.

Thanks in advance.

网友答案:
#!/usr/bin/ruby1.8

class Foo

  def Foo.bar
    puts "bar"
  end

end

Foo.bar    # => bar

class <<Foo
  remove_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 eigenclass. Ruby can't put it in Foo, because then it would be an instance method. Ruby creates Foo's eigenclass (aka "singleton class"), sets the superclass of the eigenclass to Foo's superclass, and then sets Foo's superclass to the eigenclass:

Foo -------------> Foo(eigenclass) -------------> Object
        super      def bar             super

That's why we have to open up Foo's eigenclass using class <<Foo to remove method bar.

网友答案:

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
网友答案:

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.

网友答案:

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

网友答案:

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

网友答案:

Object.send(:remove_const, :Foo)

相关阅读:
Top