Application.rb 覆盖基础 Ruby class

Application.rb override base Ruby class

我有一个 RoR 应用程序在其 application.rb

中执行以下操作
Digest::MD5 = Digest::SHA256

这反过来确保每次调用 Digest::MD5 时,它都会用 Digest::SHA256 替换结果。我相信这会产生一些意想不到的后果,例如难以调试的运行时问题。除了这种方法还有其他选择吗?

我觉得很危险。我知道想在 MD5 上使用 SHA256,但它们绝对不能互操作,并且 MD5 肯定有良性用途,您的某些依赖项可能会使用它。

相反,如果使用 MD5,为什么不使用周围别名发出警告?

class Digest::MD5
   alias :orig_hexencode :hexencode

  def hexencode(str)
    Rails.logger.warn("Hexencode called #{}")
    puts caller

    orig_hexencode(str)
  end
end