[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

What does 'Monkey Patching' exactly Mean in Ruby?

Yaser Sulaiman

12/27/2008 3:17:00 PM

[Note: parts of this message were removed to make it a legal post.]

According to Wikipedia, a monkey patch[1] is:

> 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:

> 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.

I would like to know the exact meaning of monkey patching in Ruby. To be
more specific, I would like to know the answers to the following questions:

1- What does "runtime code" refer to in Ruby?

2- Are "monkey patching" and "open classes" different terms for the same
thing?

3- Is the following considered as monkey patching, or is it something else?

class String
def foo
"bar"
end
end

Regards,
Yaser Sulaiman

P.S. I originally posted this question on stackoverflow[2]. There are some
good
answers over there, but I'm still kinda confused. I hope that someone in the
Ruby
community can clarify this issue for me.

[1]: http://en.wikipedia.org/wiki/Mo...
[2]:
http://stackoverflow.com/questions/394144/what-does-monkey-patching-exactly-me...

32 Answers

Robert Dober

12/27/2008 3:49:00 PM

0

On Sat, Dec 27, 2008 at 4:17 PM, Yaser Sulaiman <yaserbuntu@gmail.com> wrot=
e:
> According to Wikipedia, a monkey patch[1] is:
>
>> 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:
>
>> 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.
>
> I would like to know the exact meaning of monkey patching in Ruby. To be
> more specific, I would like to know the answers to the following question=
s:
>
> 1- What does "runtime code" refer to in Ruby?
>
> 2- Are "monkey patching" and "open classes" different terms for the same
> thing?
I guess this is the critical point why the Wikipedia article says
"misunderstood"
as in Ruby even core classes are open MP just does not really mean
anything else for core classes
and your own classes or any other imported classes.
>
> 3- Is the following considered as monkey patching, or is it something els=
e?
>
> class String
> def foo
> "bar"
> end
> end

I would consider it MP
> Regards,
> Yaser Sulaiman
>
> P.S. I originally posted this question on stackoverflow[2]. There are som=
e
> good
> answers over there, but I'm still kinda confused. I hope that someone in =
the
> Ruby
> community can clarify this issue for me.
Hopefully ;)
Cheers
Robert
>
> [1]: http://en.wikipedia.org/wiki/Mo...
> [2]:
> http://stackoverflow.com/questions/394144/what-does-monkey-patch...
ly-mean-in-ruby
>



--=20
Il computer non =E8 una macchina intelligente che aiuta le persone
stupide, anzi, =E8 una macchina stupida che funziona solo nelle mani
delle persone intelligenti.
Computers are not smart to help stupid people, rather they are stupid
and will work only if taken care of by smart people.

Umberto Eco

David A. Black

12/27/2008 3:57:00 PM

0

Hi --

On Sun, 28 Dec 2008, Yaser Sulaiman wrote:

> According to Wikipedia, a monkey patch[1] is:
>
>> 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:
>
>> 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.
>
> I would like to know the exact meaning of monkey patching in Ruby.

There's no exact meaning. Some people use it to mean re-opening core
classes; some people use it to mean re-opening core classes in an
incompetent, unsafe manner; some people use it as a way of indicating
that any time you re-open a core class, you shouldn't (which is a
well-intentioned but reductive position); some people use it to mean
re-opening non-core classes; and so on.

It's basically a negative, uninformative, rather silly term that has
nothing going for it. I personally don't use it. I'm a minority of
about one, though :-)


David

--
David A. Black / Ruby Power and Light, LLC
Ruby/Rails consulting & training: http://www.r...
Coming in 2009: The Well-Grounded Rubyist (http://manning....)

Phlip

12/27/2008 3:58:00 PM

0

Yaser Sulaiman wrote:

> 1- What does "runtime code" refer to in Ruby?

In WikiPedia's world, all applications have code that executes for the end-user,
and support code - such as compilers, test files, code generators, etc. Runtime
code is the former.

> 3- Is the following considered as monkey patching, or is it something else?
>
> class String
> def foo
> "bar"
> end
> end

No, that is simply extending the class with a new method. Ruby classes are
always open for extension, and the article you cited implied the
misunderstanding was that is monkey patching. It is not.

This is monkey patching:

class String
def length
42
end
end

The method .length already existed, but we had our way with it. And this example
shows why MP is kind'a dangerous. Ruby has no mechanism to allow only our own
modules to see our patches. My example gives all library code a bogus string
length, and our runtime code would not last very long...

Unfortunately for those who fear Monkey Patching, many Ruby libraries rely on
it. They are released rough-and-ready, before any serious effort to detect where
to install "hooks" (look up "Abstract Template Design Pattern"). So to get
anything done in a big Ruby application, your monkeys will be frequently
climbing up your inheritance trees and patching the code they find there!

> [1]: http://en.wikipedia.org/wiki/Mo...

WP is full of biases and points-of-view; use it carefully!!

--
Phlip

Robert Klemme

12/27/2008 4:03:00 PM

0

On 27.12.2008 16:17, Yaser Sulaiman wrote:
> [Note: parts of this message were removed to make it a legal post.]
>
> According to Wikipedia, a monkey patch[1] is:
>
>> 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:
>
>> 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.
>
> I would like to know the exact meaning of monkey patching in Ruby. To be
> more specific, I would like to know the answers to the following questions:

<disclaimer>I am by no means authoritative for the subject.</disclaimer>

> 1- What does "runtime code" refer to in Ruby?

It refers to the code that is shipped with the interpreter or other
runtime system (e.g. String, Hash, Array, Fixnum and the like). Usually
a lot other code depends on this and a monkey patch has a certain
potential to wreck havoc on the whole application. :-)

> 2- Are "monkey patching" and "open classes" different terms for the same
> thing?

Yes, of course. "Open classes" is a general concept, while "monkey
patching" is a methodology (at least something procedural). You can use
"open classes" to do "monkey patching" - but there are other tools as
well that you can use to do it (e.g. you could try to overwrite code in
memory or in C change the pointer of a basic function like strlen).

> 3- Is the following considered as monkey patching, or is it something else?
>
> class String
> def foo
> "bar"
> end
> end

I would not call this "monkey patching" because although you modify a
core class you do not modify a _core functionality_. Most of the time I
have seen "monkey patching" denote a change to core functionality with
potentially far reaching consequences, e.g. this would rather be a
monkey patch

class Object
def to_s
"boom!"
end
end

because it has potential to make certain things work but also break *a
lot* of code.

Cheers

robert

--
remember.guy do |as, often| as.you_can - without end

Robert Klemme

12/27/2008 4:49:00 PM

0

2008/12/27 David A. Black <dblack@rubypal.com>:

> It's basically a negative, uninformative, rather silly term that has
> nothing going for it. I personally don't use it. I'm a minority of
> about one, though :-)

I'd say "about two". :-)

robert


--
remember.guy do |as, often| as.you_can - without end

James Britt

12/27/2008 6:18:00 PM

0

David A. Black wrote:

> It's basically a negative, uninformative, rather silly term that has
> nothing going for it. I personally don't use it. I'm a minority of
> about one, though :-)

Minority of at least two.



--
James Britt


Martin DeMello

12/27/2008 9:43:00 PM

0

On Sat, Dec 27, 2008 at 8:49 PM, Robert Klemme
<shortcutter@googlemail.com> wrote:
> 2008/12/27 David A. Black <dblack@rubypal.com>:
>
>> It's basically a negative, uninformative, rather silly term that has
>> nothing going for it. I personally don't use it. I'm a minority of
>> about one, though :-)
>
> I'd say "about two". :-)

Four at the least :)

m.

James Britt

12/27/2008 11:16:00 PM

0

Martin DeMello wrote:
> On Sat, Dec 27, 2008 at 8:49 PM, Robert Klemme
> <shortcutter@googlemail.com> wrote:
>> 2008/12/27 David A. Black <dblack@rubypal.com>:
>>
>>> It's basically a negative, uninformative, rather silly term that has
>>> nothing going for it. I personally don't use it. I'm a minority of
>>> about one, though :-)
>> I'd say "about two". :-)
>
> Four at the least :)

Cool! We can be a gang!




--
James Britt

www.happycamperstudios.com - Wicked Cool Coding
www.jamesbritt.com - Playing with Better Toys
www.ruby-doc.org - Ruby Help & Documentation
www.rubystuff.com - The Ruby Store for Ruby Stuff

marc

12/28/2008 11:17:00 AM

0

David A. Black said...
> Hi --
>
> On Sun, 28 Dec 2008, Yaser Sulaiman wrote:
>
> > According to Wikipedia, a monkey patch[1] is:
> >
> >> 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:
> >
> >> 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.
> >
> > I would like to know the exact meaning of monkey patching in Ruby.
>
> There's no exact meaning. Some people use it to mean re-opening core
> classes; some people use it to mean re-opening core classes in an
> incompetent, unsafe manner; some people use it as a way of indicating
> that any time you re-open a core class, you shouldn't (which is a
> well-intentioned but reductive position); some people use it to mean
> re-opening non-core classes; and so on.

My brain is fuddled today recovering from flu, but what do you mean by
"reopening"?

--
Cheers,
Marc


David A. Black

12/28/2008 11:19:00 AM

0

Hi --

On Sun, 28 Dec 2008, marc wrote:

> David A. Black said...
>> Hi --
>>
>> On Sun, 28 Dec 2008, Yaser Sulaiman wrote:
>>
>>> According to Wikipedia, a monkey patch[1] is:
>>>
>>>> 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:
>>>
>>>> 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.
>>>
>>> I would like to know the exact meaning of monkey patching in Ruby.
>>
>> There's no exact meaning. Some people use it to mean re-opening core
>> classes; some people use it to mean re-opening core classes in an
>> incompetent, unsafe manner; some people use it as a way of indicating
>> that any time you re-open a core class, you shouldn't (which is a
>> well-intentioned but reductive position); some people use it to mean
>> re-opening non-core classes; and so on.
>
> My brain is fuddled today recovering from flu, but what do you mean by
> "reopening"?

Like this:

class Array
def my_new_method
end
end

where I'm reopening the core Array class and adding and/or overriding
methods in it.


David

--
David A. Black / Ruby Power and Light, LLC
Ruby/Rails consulting & training: http://www.r...
Coming in 2009: The Well-Grounded Rubyist (http://manning....)