Skip to content

Instantly share code, notes, and snippets.

@andreyuhai
Created February 15, 2020 20:06
Show Gist options
  • Save andreyuhai/268a5c74a80ef53ae1f99866ecf6a155 to your computer and use it in GitHub Desktop.
Save andreyuhai/268a5c74a80ef53ae1f99866ecf6a155 to your computer and use it in GitHub Desktop.
Ruby on Rails requiring custom script

Source: Love Your lib Directory By: Brian Cardarella

Using lib/ to extend core, stlib, or a gem

Far too often I’ve needed to extend a class that is being defined outside of my project. There are a few ways to deal with this. You can use a Composite to define a new class that you can then play around with. The downside to this is that I sometimes want to modify a class that is being inherited by other classes. This is when I think it is appropriate to Monkey Patch.

The pattern I have fallen upon is to define a gem_ext/ directory and a gem_ext.rb file in lib. I then make sure the extensions are loaded up using an initializer. For lack of a better term I call this lib_loader.rb. Lets start with the loader.

# config/initializers/lib_loader.rb

require 'gem_ext'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment