Search code examples
rubymonkeypatching

How to apply refinements / monkey-patches globally via environment.rb?


Lets take the example from the Rainbow gem:

using Rainbow allows to color string objects directly like so 'Hi'.green.

However, when I tried to add using Rainbow to environment.rb in the hopes of being able to access the monkey-patched coloring methods in any file the environment.rb is required in, seems like the coloring methods are not available.

Is there a way to apply the monkey-patch globally once instead of doing using Rainbow in each file separately?


Solution

  • No.

    That is literally the whole point of Refinements. If that were possible, Refinements would be fundamentally b0rken.

    Refinements are only active in the lexical scope they are used in.