ArticleslgStudy

science

Monkey patch

Monkey patch is a science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Monkey patch rather than just read about it. In short: Monkey patch is the act of dynamically modifying the runtime code (not the source code) of a dynamic programming language, and it is the information (data/code) used to modify the runtime code. Monkey patching adds or replaces programming aspects like methods, classes, attributes, and functions in memory.

Key takeaways

  • Monkey patch belongs to science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Monkey patch to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Monkey patch from memory before moving on to harder problems.

Reference excerpt

Monkey patch is the act of dynamically modifying the runtime code (not the source code) of a dynamic programming language, and it is the information (data/code) used to modify the runtime code. Monkey patching adds or replaces programming aspects like methods, classes, attributes, and functions in memory. Modifying the runtime code allows for modifying the behavior of third-party software without maintaining a modified version of the source code. The term monkey patch seems to have come from an earlier term, guerrilla patch, which referred to changing code sneakily – and possibly incompatibly with other such patches – at runtime. The word guerrilla, nearly homophonous with gorilla, became monkey, possibly to make the patch sound less intimidating. Despite the name's suggestion, a monkey patch is sometimes the official method of extending a program. For example, web browsers such as Firefox and Internet Explorer used to encourage this, although today browsers (including Firefox) support extension differently. Monkey patch varies depending upon context. In Ruby, Python, and other languages, monkey patch refers only to dynamic modification of a class or module at runtime, motivated by the intent to patch existing third-party code as a workaround to a bug or feature which does not act as desired. Other forms of modifying classes at runtime have different names. For example, in Zope and Plone, security patches are often delivered using dynamic class modification, but they are called hot fixes.

Pitfalls Some pitfalls of monkey patching:

Incompatibility A new release of the patched software may break the patch. For this reason, a monkey patch may be conditional and thus only applied if appropriate. Overwriting If the same method is patched multiple times, then only the last one is used; the other patches have no effect, unless monkey patches are written with a pattern like alias_method_chain. Confusion A monkey patch creates a discrepancy between the source code and actual behavior that can confuse developers. For example, the Linux kernel detects proprietary and other third-party modules such as the Nvidia driver, which tamper with kernel structures, so that developers will not waste their time trying to debug a problem that they cannot fix. Chaos A monkey patch can contain malicious code that attacks the program, or other patches. For example, in 2009, Giorgio Maone, developer of NoScript, attacked the Adblock Plus extension for Firefox, adding exceptions so that advertisements on his websites would work. The offending code also made sure that if the user attempted to remove the exceptions, they would be added again. An escalating war ensued with new adblock rules pushed to users, followed by Maone sabotaging them, which eventually led to Mozilla stepping in to change policies regarding add-ons.

Examples The following monkey patches the value of pi in the standard Python math library to make it compliant with the Indiana pi bill.

The next time Python is started, the value of pi will be what it was before the patch: 3.141592653589793.

See also

Advice (programming) Aspect-oriented programming Dynamic loading Extension method Objective-C category Polyfill Self-modifying code

References

Worked examples

Example 1 — a first encounter with Monkey patch

Start with the simplest possible case. Write down what Monkey patch claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Monkey patch before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Monkey patch ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Monkey patch

In research
Monkey patch appears in science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Monkey patch in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Monkey patch is common in secondary-school and first-year university syllabi. It links to neighbouring topics Object-oriented programming, Programming constructs, so understanding it makes those chapters shorter.
In everyday life
Look for Monkey patch outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Monkey patch” →

How to study Monkey patch in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Monkey patch means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Monkey patch out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Monkey patch in simple terms?

Monkey patch is the act of dynamically modifying the runtime code (not the source code) of a dynamic programming language, and it is the information (data/code) used to modify the runtime code. Monkey patching adds or replaces programming aspects like methods, classes, attributes, and functions in…

Why does Monkey patch matter?

Because it connects several science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Monkey patch?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Monkey patch.

Tags

  • Object-oriented programming
  • Programming constructs

Keep exploring