在 ruby 中:
module Example
class ExampleValidator
def mymethod
# do something important
end
end
end
是否可以使 mymethod
不可变(即没有猴子修补)?
否则“卡住”整个类ExampleValidator
也是可以接受的。
最佳答案
是的,您可以调用freeze
在模块上。
Example::ExampleVaildator.freeze
但是,I'd question why this is necessary 。也许这是用技术问题来解决社会问题。诸如Rubocop之类的工具应警告猴子补丁。
关于ruby - 是否可以在 Ruby 中卡住(使其不可变)类或方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71180524/