# Organizing monkey patches in environment.rb?

**URL:** <https://www.ruby-forum.com/t/organizing-monkey-patches-in-environment-rb/131098>\
**Category:** Rails\
**Created:** [March 6, 2008, 12:35am UTC](https://www.ruby-forum.com/t/organizing-monkey-patches-in-environment-rb/131098 "2008-03-06T00:35:45Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [March 6, 2008, 12:35am UTC](https://www.ruby-forum.com/t/organizing-monkey-patches-in-environment-rb/131098/1 "2008-03-06T00:35:45Z")

</div>

I have been monkey patching directly in environment.rb for a while now.

I’m at a point where I’d like to move all of this code out of  
environment.rb into one or more files with meaningful names so as to  
keep environment.rb a reasonable size.

Anyone decided to do this and willing to share advice on how they  
organized their extracted monkey patches?

Perhaps put each “monkey patch” file into RAILS\_ROOT/lib and requiring  
them from environment.rb? Something else?

All ears,  
Wes

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [March 6, 2008, 1:25am UTC](https://www.ruby-forum.com/t/organizing-monkey-patches-in-environment-rb/131098/2 "2008-03-06T01:25:24Z")

</div>

Hi –

On Thu, 6 Mar 2008, Wes G. wrote:

> Perhaps put each “monkey patch” file into RAILS\_ROOT/lib and requiring  
> them from environment.rb? Something else?

I acknowledge the term “monkey patching” only under protest 🙂 (I  
hate it, and never quite know what it means anyway.) But have a look,  
in Rails 2.0 applications, at config/initializers. It’s a directory  
where you can offload environment.rb stuff and have it automatically  
loaded.

David

–  
Upcoming Rails training from David A. Black and Ruby Power and Light:  
ADVANCING WITH RAILS, April 14-17 2008, New York City  
CORE RAILS, June 24-27 2008, London (Skills Matter)  
See [http://www.rubypal.com](http://www.rubypal.com) for details. Berlin dates coming soon!

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [March 6, 2008, 12:17pm UTC](https://www.ruby-forum.com/t/organizing-monkey-patches-in-environment-rb/131098/3 "2008-03-06T12:17:02Z")

</div>

On Mar 5, 2008, at 7:24 PM, David A. Black wrote:

> > Anyone decided to do this and willing to share advice on how they  
> > loaded.
> 
> David
> 
> –  
> Upcoming Rails training from David A. Black and Ruby Power and Light:  
> ADVANCING WITH RAILS, April 14-17 2008, New York City  
> CORE RAILS, June 24-27 2008, London (Skills Matter)  
> See [http://www.rubypal.com](http://www.rubypal.com) for details. Berlin dates coming soon!

I’ve collected these bits in separate files under RAILS\_ROOT/lib/ext/

-rw-r–r-- 1 rab rab 63 Jan 22 14:17 lib/ext/array.rb  
-rw-r–r-- 1 rab rab 3302 Feb 26 19:23 lib/ext/net\_http\_post.rb  
-rw-r–r-- 1 rab rab 187 Jan 4 14:29 lib/ext/nil\_class.rb  
-rw-r–r-- 1 rab rab 131 Dec 11 12:20 lib/ext/string.rb  
-rw-r–r-- 1 rab rab 1601 Jan 28 09:55 lib/ext/time.rb

And then arrange form to be required within config/environment.rb with:

Dir[File.join(RAILS\_ROOT, ‘lib’, ‘ext’, ‘\*.rb’)].each do |f|  
base = File.basename(f, ‘.rb’)  
require “ext/#{base}”  
end

I haven’t started any projects in Rails 2.x yet, but will check out  
the initializers when I do.

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid
