# Class\_eval better than reopening classes?

**URL:** <https://www.ruby-forum.com/t/class-eval-better-than-reopening-classes/139112>\
**Category:** Rails\
**Created:** [May 30, 2008, 10:50pm UTC](https://www.ruby-forum.com/t/class-eval-better-than-reopening-classes/139112 "2008-05-30T22:50:46Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![squeegy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/dec6dc/32.png) [@squeegy](https://www.ruby-forum.com/u/squeegy)\
**Post date:** [May 30, 2008, 10:50pm UTC](https://www.ruby-forum.com/t/class-eval-better-than-reopening-classes/139112/1 "2008-05-30T22:50:46Z")

</div>

I came accross this snippet from the docrails project

“When monkey-patching existing classes it’s often better to use  
`class_eval` instead of opening the class directly.”

From this commit:  
[http://github.com/lifo/docrails/commit/60e37868e40ac11f07cbda81f24dcdb0a9c68783#L0R275](http://github.com/lifo/docrails/commit/60e37868e40ac11f07cbda81f24dcdb0a9c68783#L0R275)

First of all, is this right? And second, why? Does it make a  
difference if this is a in the context of a rails plugin?

---

<div class="post-metadata">

**Author:** ![squeegy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/dec6dc/32.png) [@squeegy](https://www.ruby-forum.com/u/squeegy)\
**Post date:** [May 30, 2008, 11:15pm UTC](https://www.ruby-forum.com/t/class-eval-better-than-reopening-classes/139112/2 "2008-05-30T23:15:31Z")

</div>

On May 30, 9:50 pm, Alex W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> I came accross this snippet from the docrails project
> 
> “When monkey-patching existing classes it’s often better to use  
> `class_eval` instead of opening the class directly.”
> 
> From this commit:[http://github.com/lifo/docrails/commit/60e37868e40ac11f07cbda81f24dcd](http://github.com/lifo/docrails/commit/60e37868e40ac11f07cbda81f24dcd)…

> **[Dynamically created methods in Ruby](http://ola-bini.blogspot.com/2008/05/dynamically-created-methods-in-ruby.html)**
>
> There seems to be some confusion with regards to dynamically defining methods in Ruby. I thought I'd take a look at the three available meth...

says the opposite

Fred
