Upgrading Your Rails App: Shims and Monkey Patches Explained

Pablo Adell
3 min readJan 21, 2023

--

Photo by Jamie Haughton on Unsplash

Upgrading your Rails App with Shims and Monkey Patches

Maintaining an up-to-date Rails version is essential for the performance, security, and scalability of your application.

However, as Ruby/Rails advances, deprecated methods and past version functionality may need to be moved to shims and monkey patches.

This guide will provide an overview of what shims and monkey patches are, why they are important for upgrading your Rails version, and the best practices for utilizing them in your application.

What are Shims and Monkey Patches?

Shims and monkey patches are time-honored methods of applying quick fixes to code when upgrading a Rails version. Shims are essentially wrappers around existing code that allow old code to be executed without any changes.

Monkey patches go even deeper, modifying existing code to provide new behavior. While shims and monkey patches can provide a cost-effective and quick solution to the problem of rearranging code when upgrading a Rails version, there are inherent risks with using them.

Why are Shims and Monkey Patches Important for Upgrading your Rails Version?

Shims and monkey patches can be useful ways to quickly apply custom changes to the framework code without having to go into every detail during each upgrade cycle. This also makes them popular methods for improving the performance and scalability of an application while avoiding major changes.

A good example is a feature that was deprecated in a previous version of Rails and no longer appears in current versions. If the feature is important to an application, a shim or monkey patch can be used to revive it and make it work in the new version.

Best Practices for Utilizing Shims and Monkey Patches

Before utilizing shims and monkey patches during an upgraded cycle, it’s important to remember that they should only be used as a temporary solution while a better, more permanent fix is put in place.

When using a shim or a monkey patch, take the time to understand exactly how the underlying framework behaves. This will help to ensure that the fix being applied is valid and does not override any existing functionality.

It’s also important to understand the risks associated with using shims and monkey patches. While these methods can be cost-effective and quick-to-implement solutions for upgrading your Rails version, they may also introduce unexpected behavior and security issues.

Conclusion

Upgrading a Rails app is an essential process for ensuring continued productivity and avoiding errors caused by incompatibilities with newer versions. Shims and monkey patches are two techniques that can be used to ensure that the application works correctly in all versions.

References

For further information and best practices on upgrading your Rails version with shims and monkey patches, I recommend the following books:

- Agile WebApp Development with Rails 6 — Sam Ruby, David Copeland

- Ruby on Rails Tutorial: Learn Web Development with Rails (Addison-wesley Professional Ruby) — Michael Hartl

- Rails 5 Way, The (Addison-Wesley Professional Ruby Series) — Obie Fernandez

--

--

Pablo Adell

Hello there!! My name is Pablo, I am a Full-Stack Software Engineer at Affirm, focused mainly in Ruby on Rails and React. I hope you enjoy my content!