I’m trying to dynamically undefine a method by opening up the eigenclass of the class. I want something like:
def remove_defined_mock_name_method(name)
if Settings.respond_to?(name)
class << Settings
remove_method name
end
end
end
where ‘name’ is a symbol. Problem is name is not available to the reopened class. How can I achieve undefining a class method dynamically like this through a method call?
For those curious, the use case is that I want to undefine a dynamically defined method after each test in rspec for rails.
nameis a local variable. Local variables are local to the scope they are defined in, that’s why they are called local variables. The only construct in Ruby that creates a nested scope is a block, so you would have to use a block if you want to have access toname, i.e. by usingclass_evalor something like that.But in this case, that’s not necessary: