[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Monkey Patching (definition)?

Christoph Schiessl

5/16/2008 6:28:00 PM

For example. Look the following piece of simple Ruby Code:

class ExampleClass
def some_instance_method
'Called some_instace_method!'
end
end

def ExampleClass.i_am_a_class_method
'Called i_am_a_class_method!'
end

inst = ExampleClass.new
def inst.i_am_an_instance_method
'Called i_am_an_instance_method'
end

Basically, I am reopening the definition of ExampleClass and adding
another method. At first I am creating a new class method and
afterwards I am creating a new instance method (for the object inst
only). Is monkey patching the correct term for that kind of
programming style?

How would you call this style of programming? I'm asking because I'm
quite new to Ruby and currently trying to learn the right vocabulary
to easily communicate with other Ruby programmers. I am definitely not
looking for flame war!

Best regards,
Christoph Schiessl

11 Answers

Jason Roelofs

5/16/2008 6:47:00 PM

0

On 5/16/08, Christoph Schiessl <c.schiessl@gmx.net> wrote:
> For example. Look the following piece of simple Ruby Code:
>
> class ExampleClass
> def some_instance_method
> 'Called some_instace_method!'
> end
> end
>
> def ExampleClass.i_am_a_class_method
> 'Called i_am_a_class_method!'
> end
>
> inst = ExampleClass.new
> def inst.i_am_an_instance_method
> 'Called i_am_an_instance_method'
> end
>
> Basically, I am reopening the definition of ExampleClass and adding another
> method. At first I am creating a new class method and afterwards I am
> creating a new instance method (for the object inst only). Is monkey
> patching the correct term for that kind of programming style?
>
> How would you call this style of programming? I'm asking because I'm quite
> new to Ruby and currently trying to learn the right vocabulary to easily
> communicate with other Ruby programmers. I am definitely not looking for
> flame war!
>
> Best regards,
> Christoph Schiessl
>
>

That's really just open classes. The term Monkey Patching comes into
play when you're doing the same with classes you haven't written or
don't have control over, say String or Array.

class Array
def my_method
puts "hi"
end
end

array = []
array.my_method # => "hi"

Jason

Phillip Gawlowski

5/16/2008 6:59:00 PM

0

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Christoph Schiessl wrote:

|
| Basically, I am reopening the definition of ExampleClass and adding
| another method. At first I am creating a new class method and afterwards
| I am creating a new instance method (for the object inst only). Is
| monkey patching the correct term for that kind of programming style?

Yes and no. Monkey patching it is called, if one ignores potential side
effects, or performs something dangerous, like re-opening a a Core class.

There was a long discussion on the topic in this thread:
<http://groups.google.com/group/ruby-talk-google/browse_thread/thread/1900b19f4db151f6/6eaff6fb73...

| How would you call this style of programming? I'm asking because I'm
| quite new to Ruby and currently trying to learn the right vocabulary to
| easily communicate with other Ruby programmers. I am definitely not
| looking for flame war!

Probably 'dynamic'. :P


- --
Phillip Gawlowski
Twitter: twitter.com/cynicalryan
Blog: http://justarubyist.bl...

Use statement labels that mean something.
~ - The Elements of Programming Style (Kernighan & Plaugher)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.8 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail....

iEYEARECAAYFAkgt2VYACgkQbtAgaoJTgL+FhACdEWCS+csnXHPPu675BgPaMQ/8
RUsAnj8bnNQfBBDELgjMATUDwzYRSXno
=VF10
-----END PGP SIGNATURE-----

Robert Dober

5/16/2008 7:31:00 PM

0

On Fri, May 16, 2008 at 8:58 PM, Phillip Gawlowski
<cmdjackryan@googlemail.com> wrote:

> Yes and no. Monkey patching it is called, if one ignores potential side
> effects, or performs something dangerous, like re-opening a a Core class.
>
Sorry Philip to disagree slightly, re-opening a Core class is not
dangerous it is potentially dangerous.
I would it call dangerous in libraries not applications and still
there are libraries specifically made to do this as e.g. Facets.
Ok as OP is a nuby maybe we might say dangerous after all, but not
tabou at least ;).
> | How would you call this style of programming? I'm asking because I'm
> | quite new to Ruby and currently trying to learn the right vocabulary to
> | easily communicate with other Ruby programmers. I am definitely not
> | looking for flame war!
Hey if you were looking for a flame war, this is not the best place
anyway ;), no worries your question is a sensitive one!
>
> Probably 'dynamic'. :P
Absolutely agree here
>
Robert
--
http://ruby-smalltalk.blo...

---
Whereof one cannot speak, thereof one must be silent.
Ludwig Wittgenstein

Justin Collins

5/16/2008 7:40:00 PM

0

Christoph Schiessl wrote:
> For example. Look the following piece of simple Ruby Code:
>
> class ExampleClass
> def some_instance_method
> 'Called some_instace_method!'
> end
> end
>
> def ExampleClass.i_am_a_class_method
> 'Called i_am_a_class_method!'
> end
>
> inst = ExampleClass.new
> def inst.i_am_an_instance_method
> 'Called i_am_an_instance_method'
> end
>
> Basically, I am reopening the definition of ExampleClass and adding
> another method. At first I am creating a new class method and
> afterwards I am creating a new instance method (for the object inst
> only). Is monkey patching the correct term for that kind of
> programming style?
>
> How would you call this style of programming? I'm asking because I'm
> quite new to Ruby and currently trying to learn the right vocabulary
> to easily communicate with other Ruby programmers. I am definitely not
> looking for flame war!
>
> Best regards,
> Christoph Schiessl
>

I would suggest open classes, re-opening classes, and creating the so
called 'singleton' classes (another debate there, you can easily search
for discussions on it). The term "monkey patching" seems to come from
the Python community, where it is not as openly accepted as it is here,
and thus appears to carry a stronger negative connotation.

Just my opinion, though.

-Justin

Florian Gilcher

5/16/2008 7:49:00 PM

0

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1


On May 16, 2008, at 9:31 PM, Robert Dober wrote:

> On Fri, May 16, 2008 at 8:58 PM, Phillip Gawlowski
> <cmdjackryan@googlemail.com> wrote:
>
>> Yes and no. Monkey patching it is called, if one ignores potential
>> side
>> effects, or performs something dangerous, like re-opening a a Core
>> class.
>>
> Sorry Philip to disagree slightly, re-opening a Core class is not
> dangerous it is potentially dangerous.
> I would it call dangerous in libraries not applications and still
> there are libraries specifically made to do this as e.g. Facets.
> Ok as OP is a nuby maybe we might say dangerous after all, but not
> tabou at least ;).
>> | How would you call this style of programming? I'm asking because
>> I'm
>> | quite new to Ruby and currently trying to learn the right
>> vocabulary to
>> | easily communicate with other Ruby programmers. I am definitely not
>> | looking for flame war!
> Hey if you were looking for a flame war, this is not the best place
> anyway ;), no worries your question is a sensitive one!
>>
>> Probably 'dynamic'. :P
> Absolutely agree here
>>
> Robert
> --

Actually, I disagree with both definitions. I don't consider opening
and extending
Classes (be they core or not) monkey-patching.

I define monkey-patching as "opening and changing behaviour of a
method/class
in the knowledge that other classes/libraries depend on it".

So

class Array
def my_funky_function
"this array is of the hook"
end
end

has nothing to do with monkey-patching - chances are high that there
are no conflicts.

class Array
def []=(elem)
#whoops, i dropped the element
end
end

is a total different case. I postulate that most Programs written in
Ruby depend on the behaviour
of Array#[]=. So this is monkey-patching at its worst. (you patch a
certain functionality because
you think that it should behave differently. In this case, it is
pretty obvious, that this try was a failure.

The wikipedia has a nice explanation:

"In Python, the term monkey patch only refers to dynamic modifications
of a class at runtime based on the intent to patch existing methods in
an external class as a workaround to a bug or feature which does not
act as you desire. Other forms of modifying a class at runtime have
different names, based on their different intents. For example, in
Zope and Plone, security patches are often delivered using dynamic
class modification, but they are called hot fixes.

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

Regards
Florian Gilcher
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.8 (Darwin)

iEYEARECAAYFAkgt5QoACgkQJA/zY0IIRZbRWQCdG0vRaJ4pNCCVTQsa2lgKbs0B
cy0AnjlxblREs1mQhmqn6WEkPP/HjEqr
=fQOe
-----END PGP SIGNATURE-----

ThoML

5/16/2008 8:16:00 PM

0

> There was a long discussion on the topic in this thread:
> <http://groups.google.com/group/ruby-talk-google/browse_thread/thre...

And there even is a wikipedia article on MP:
http://en.wikipedia.org/wiki/Monke...

Robert Dober

5/16/2008 9:04:00 PM

0

On Fri, May 16, 2008 at 9:48 PM, Florian Gilcher <flo@andersground.net> wrote:
> -----BEGIN PGP SIGNED MESSAGE-----

>
> Actually, I disagree with both definitions. I don't consider opening and
> extending
> Classes (be they core or not) monkey-patching.
Nor do I, as Philip was kindly giving the link to the recent
discussion in which I have been more and more driven away from the
term MP, by the kind and thoughtful words of David Black I did not
want to repeat this POV.
<some other interesting stuff skipped>
Cheers
Robert

--
http://ruby-smalltalk.blo...

---
Whereof one cannot speak, thereof one must be silent.
Ludwig Wittgenstein

Avdi Grimm

5/16/2008 9:20:00 PM

0

On Fri, May 16, 2008 at 3:48 PM, Florian Gilcher <flo@andersground.net> wrote:
> I define monkey-patching as "opening and changing behaviour of a
> method/class
> in the knowledge that other classes/libraries depend on it".

I think this is one of the best definitions I've seen. It should be
remembered that the term arose in specific circumstances: it came from
the practice of dynamically modifying the behavior of *existing* code.
Hence "patching", rather than "monkey extending" or "monkey adding".

Of course, it's not as clear-cut as all that because sometimes one
coder's extension is another coder's unexpected change in behavior.
But the spirit of monkey patching is that of altering existing code so
that it behaves differently - to temporarily fix a bug without
altering the source, for instance.

--
Avdi

Home: http:...
Developer Blog: http:.../devblog/
Twitter: http://twitte...
Journal: http://avdi.livej...

Rick DeNatale

5/16/2008 10:44:00 PM

0

On Fri, May 16, 2008 at 2:27 PM, Christoph Schiessl <c.schiessl@gmx.net> wrote:
> For example. Look the following piece of simple Ruby Code:
>
> class ExampleClass
> def some_instance_method
> 'Called some_instace_method!'
> end
> end
>
> def ExampleClass.i_am_a_class_method
> 'Called i_am_a_class_method!'
> end
>
> inst = ExampleClass.new
> def inst.i_am_an_instance_method
> 'Called i_am_an_instance_method'
> end
>
> Basically, I am reopening the definition of ExampleClass and adding another
> method. At first I am creating a new class method and afterwards I am
> creating a new instance method (for the object inst only). Is monkey
> patching the correct term for that kind of programming style?

Actually, this code never reopens the definition of ExampleClass.

def ExampleClass.i_am_a_class_method
end

Doesn't reopen example class, it defines a method on the singleton
class (a.k.a metaclass) of the object bound to the global name
ExampleClass, which just happens to be a class, it doesn't really do
anything to the ExampleClassObject.

inst = ExampleClass.new
def inst.i_am_an_instance_method
end

Likewise doesn't touch the ExampleClass object at all either. It adds
a method to the singleton class of the object bound to the variable
inst.

I've skimmed the earlier responses and I don't think anyone else
pointed this out.

--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denh...

Todd Benson

5/16/2008 11:05:00 PM

0

On Fri, May 16, 2008 at 3:20 PM, ThoML <micathom@gmail.com> wrote:
>> There was a long discussion on the topic in this thread:
>> <http://groups.google.com/group/ruby-talk-google/browse_thread/thre...
>
> And there even is a wikipedia article on MP:
> http://en.wikipedia.org/wiki/Monke...

Interesting side note:

class C
def foo obj
'foo ' << obj.to_s
end
end

c = C.new

class C
unless respond_to?('foo')
def foo obj
'bar ' << obj.to_s
end
end
end

class Array
unless respond_to?('==')
def == obj
'arr == ' << obj.to_s
end
end
end

puts c.foo(42)
# bar 42 -- the new method ran over the old one
puts [] == 42
# false -- the new method was ignored


The parser doesn't check the unless condition for 'normal' methods.
(This is on 1.8.6)

Todd