Is there a way to avoid (raise an error when it is attempted) definition of a method with a certain name, for example Foo#bar
? (A usecase would be when Foo#bar
is already defined, and I want to avoid that method being overridden, but that is irrelevant to the question.) I am assuming something like:
class Foo
prohibit_definition :bar
end
...
# Later in some code
class Foo
def bar
...
end
end
# => Error: `Foo#bar' cannot be defined
class Class
def method_added(method_name)
raise "So sad, you can't add" if method_name == :bad
end
end
class Foo
def bad
puts "Oh yeah!"
end
end
#irb> RuntimeError: So sad, you can't add
# from (irb):3:in `method_added'
# from (irb):7
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With