Ruby 中的“猴子补丁"究竟是什么意思? [英] What does 'Monkey Patching' exactly Mean in Ruby?

查看:37
本文介绍了Ruby 中的“猴子补丁"究竟是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据维基百科,猴子补丁是:

一种扩展或修改运行时的方法动态语言代码 [...]不改变原始来源代码.

a way to extend or modify the runtime code of dynamic languages [...] without altering the original source code.

来自同一条目的以下陈述让我感到困惑:

The following statement from the same entry confused me:

在 Ruby 中,术语猴子补丁是被误解为任何动态对类的修改,通常是用作动态的同义词在运行时修改任何类.

In Ruby, the term monkey patch was misunderstood to mean any dynamic modification to a class and is often used as a synonym for dynamically modifying any class at runtime.

我想知道Ruby 中猴子补丁的确切含义.它是在执行以下操作,还是其他操作?

I would like to know the exact meaning of monkey patching in Ruby. Is it doing something like the following, or is it something else?

class String
  def foo
    "foo"
  end
end

推荐答案

简短的回答是没有确切"的含义,因为这是一个新术语,不同的人使用它的方式不同.至少可以从维基百科文章中看出这一点.有些人坚持认为它只适用于运行时"代码(我想是内置类),而有些人会用它来指代任何类的运行时修改.

The short answer is that there is no "exact" meaning, because it's a novel term, and different folks use it differently. That much at least can be discerned from the Wikipedia article. There are some who insist that it only applies to "runtime" code (built-in classes, I suppose) while some would use it to refer to the run-time modification of any class.

就个人而言,我更喜欢更具包容性的定义.毕竟,如果我们只使用术语修改内置类,我们将如何指代所有其他类的运行时修改?对我来说重要的是源代码和实际运行的类之间存在差异.

Personally, I prefer the more inclusive definition. After all, if we were to use the term for modification of built-in classes only, how would we refer to the run-time modification of all the other classes? The important thing to me is that there's a difference between the source code and the actual running class.

在 Ruby 中,术语猴子补丁是被误解为任何动态对类的修改,通常是用作动态的同义词在运行时修改任何类.

In Ruby, the term monkey patch was misunderstood to mean any dynamic modification to a class and is often used as a synonym for dynamically modifying any class at runtime.

上述声明断言 Ruby 的用法是不正确的 - 但术语会演变,这并不总是一件坏事.

The above statement asserts that the Ruby usage is incorrect - but terms evolve, and that's not always a bad thing.

这篇关于Ruby 中的“猴子补丁"究竟是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆