Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove/undef a class method

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.

like image 819
Brian Ploetz Avatar asked Jan 16 '10 23:01

Brian Ploetz


2 Answers

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.

like image 111
6 revs, 2 users 81% Avatar answered Oct 15 '22 19:10

6 revs, 2 users 81%


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
like image 19
Adrian Macneil Avatar answered Oct 15 '22 19:10

Adrian Macneil