# Safer monkey-patching?

**URL:** <https://www.ruby-forum.com/t/safer-monkey-patching/186974>\
**Category:** Ruby\
**Created:** [April 15, 2010, 5:45am UTC](https://www.ruby-forum.com/t/safer-monkey-patching/186974 "2010-04-15T05:45:45Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![exile](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/50afbb/32.png) [@exile](https://www.ruby-forum.com/u/exile)\
**Post date:** [April 15, 2010, 5:45am UTC](https://www.ruby-forum.com/t/safer-monkey-patching/186974/1 "2010-04-15T05:45:45Z")

</div>

Sometimes (or often, depending on your use) it is useful to add or  
override  
methods to an already existing class, aka monkey-patching. Would it be  
useful  
to subclass an existing object, while still using the same class-name -  
in  
essence, creating an extended local version of the class:

String = Class.new(String) do  
def size()  
return -1  
end  
end

string = String.new \<\< “foo”  
puts string.size

I realize this is re-assigning a constant, but it’s more for  
demonstration  
purposes. Now anything within lexical scope will use the new class,  
while  
other methods will use the original.

Mike

---

<div class="post-metadata">

**Author:** ![exile](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/50afbb/32.png) [@exile](https://www.ruby-forum.com/u/exile)\
**Post date:** [April 15, 2010, 1:29pm UTC](https://www.ruby-forum.com/t/safer-monkey-patching/186974/2 "2010-04-15T13:29:56Z")

</div>

On Apr 14, 11:45 pm, Mike A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Sometimes (or often, depending on your use) it is useful to add or override  
> methods to an already existing class, aka monkey-patching. Would it be useful  
> to subclass an existing object, while still using the same class-name - in  
> essence, creating an extended local version of the class:

I’d like to differentiate monkey-patching from clean extensions. While  
it may not be commonly thought of this way, to me monkey patching is  
overriding an existing method, not simply adding a new method. Hence  
monkey patching is a dangerous activity and deserves a negative  
hallmark. Where as simply adding a new method is generally safe, the  
main issue being merely potential name clashes with others doing the  
same.

> purposes. Now anything within lexical scope will use the new class, while  
> other methods will use the original.

First problem, it doesn’t work unless you use String.new to create all  
your new strings. Next problem is how to handle passing off your new  
string to external lib? Will you reconvert it to regular String to be  
on the safe side? (And of course there’s no safe guarding String’s own  
internal methods 😉

The idea has been discussed before, long time ago. I once suggested it  
as a means of selector namespaces. I believe Austin Z.'s comment  
was something about “chaos”. But I’m still not sure why he thought  
this. There is perhaps some merit to the idea. But it would certainly  
need to be explored throughly.

In any case, if you are doing something that you feel warrants this  
level of safe guarding, then just be explicit about it. Create a new  
String class: ‘class MyString \< String’.
