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.
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:
There are a few ways to access the singleton class:
class <<Foo,Foo.singleton_class,class Foo; class << selfwhich is commonly use to define class methods.Note that we used
undef_method, we could have usedremove_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.