# Open classes and language extensibility

**URL:** <https://www.ruby-forum.com/t/open-classes-and-language-extensibility/130304>\
**Category:** Ruby\
**Created:** [February 27, 2008, 7:04am UTC](https://www.ruby-forum.com/t/open-classes-and-language-extensibility/130304 "2008-02-27T07:04:49Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Martin\_DeMello](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f17d59/32.png) [@Martin\_DeMello](https://www.ruby-forum.com/u/Martin_DeMello)\
**Post date:** [February 27, 2008, 7:04am UTC](https://www.ruby-forum.com/t/open-classes-and-language-extensibility/130304/1 "2008-02-27T07:04:49Z")

</div>

I was pondering the whole “monkeypatching” debate, and I realised that  
disallowing modification of core classes removes an extremely  
important property of the language - the ability to have extension  
code you write be “on par” with the base language. This is often  
touted as one of the big advantages of lisp and forth, and it would be  
a shame to lose it in ruby. For instance, lets say your code relied  
heavily on rot13. If you couldn’t add a #rot13 method to String  
because String was a core class, you’d be forced to say rot13(string),  
or even worse, Util.rot13(string). You’d end up with expressions like  
rot13(string.capitalize.squeeze), where it is very clear which methods  
come from “base ruby” and which are yours.

## [Ruminations of a Programmer: January 2007](http://debasishg.blogspot.com/2007_01_01_archive.html) is a good read on the subject:

With a Lisp implementation, we can have a natural extension of the  
language through a macro

dolist (x '(1 2 3)) (print x) (if (evenp x) (return)))

and the moment we define the macro, it blends into the language syntax  
like a charm. This is abstraction through syntax construction.

And, the Java counterpart will be something like :

// …  
Collection\<…\> list = … ;  
CollectionUtils.dolist(list,  
new Predicate() {  
public boolean evaluate() {  
// …  
}  
});  
// …

## which provides an object oriented abstraction of the same functionality. This solution provides the necessary abstraction, but is definitely not as seamless an extension of the language as its Lisp counterpart.

martin

---

<div class="post-metadata">

**Author:** ![Martin\_DeMello](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f17d59/32.png) [@Martin\_DeMello](https://www.ruby-forum.com/u/Martin_DeMello)\
**Post date:** [February 27, 2008, 8:46am UTC](https://www.ruby-forum.com/t/open-classes-and-language-extensibility/130304/2 "2008-02-27T08:46:41Z")

</div>

Martin DeMello wrote:

> come from “base ruby” and which are yours.  
> Fully agree. Also, a method like Util.rot13(string) is defined in a  
> pretty bland namespace, so it is really almost as “global” as  
> String#rot13. How qualified would it have to be to be safe?

---

<div class="post-metadata">

**Author:** ![Martin\_DeMello](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f17d59/32.png) [@Martin\_DeMello](https://www.ruby-forum.com/u/Martin_DeMello)\
**Post date:** [February 27, 2008, 9:24am UTC](https://www.ruby-forum.com/t/open-classes-and-language-extensibility/130304/3 "2008-02-27T09:24:30Z")

</div>

On Tue, Feb 26, 2008 at 11:59 PM, Jari W.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I think this statement is incorrect. You CAN create a string with a  
> #rot13 method, without adding that method to the core class.

Well, you can add it individually to any given string, but I fail to  
see how this affects my argument.

martin

---

<div class="post-metadata">

**Author:** ![Martin\_DeMello](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f17d59/32.png) [@Martin\_DeMello](https://www.ruby-forum.com/u/Martin_DeMello)\
**Post date:** [February 27, 2008, 9:00am UTC](https://www.ruby-forum.com/t/open-classes-and-language-extensibility/130304/4 "2008-02-27T09:00:16Z")

</div>

Martin DeMello wrote:

> come from “base ruby” and which are yours.  
> I think this statement is incorrect. You CAN create a string with a  
> #rot13 method, without adding that method to the core class.

Best regards,

Jari W.

---

<div class="post-metadata">

**Author:** ![Martin\_DeMello](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f17d59/32.png) [@Martin\_DeMello](https://www.ruby-forum.com/u/Martin_DeMello)\
**Post date:** [February 27, 2008, 9:35am UTC](https://www.ruby-forum.com/t/open-classes-and-language-extensibility/130304/5 "2008-02-27T09:35:59Z")

</div>

Martin DeMello wrote:

> because String was a core class, you’d be forced to say rot13(string),

> martin

Welcome to PHP 🙂
