views:

55

answers:

2

I am doing some monkey patching in some of Ruby classes and I would like that to be included AUTOMATICALLY whenever I run ruby.

For example:

I added the method trim to String. I want to be able to do this:

ruby -e 'puts " aaaa ".trim'

I don't want to do this:

ruby -e 'require "monkey.rb"; puts " aaaa ".trim'

Is there anyway to include my monkey patches evertime I start ruby? How about irb?

Thanks!

+1  A: 

irb is probably the place where you can do this most simply. When using irb, you can use an initialization file to store anything you want run on every startup. In your home directory ("cd ~"), create a file called ".irbrc", and drop in your "require 'monkey.rb'" statement, that should do it. From then on when you start up irb, it will run anything in that script first.

Ethan Vizitei
+2  A: 

ruby and irb both take a -r option that lets you specify a library to load when running those executables. If you want to automatically load your monkey.rb library, you can start ruby with the invocation $ ruby -r monkey (assuming monkey.rb is in your $RUBYLIB path. If you don't want to do that each time, you can set up an alias in your shell config file. For example (in Bash), you could add:

alias ruby='ruby -r monkey'
mipadi
That's great man. Thanks!
Sergio Oliveira Jr.