life is too short for a diary




Fix jekyll jolt plugin using monkey patching

Tags: jekyll ruby monkey-patching

Author
Written by: Tushar Sharma
Featured image for Fix jekyll jolt plugin using monkey patching

My existing jekyll website started failing with newer jekyll version: 4.4.1. I had to do a monkey patch to resolve this error.

The build started failing

  Liquid Exception: undefined method `=~' for 1:Integer in _posts

The post responsible for the error uses jekyll-jolt.

Monkey Patching

A monkey patch is a technique in dynamic languages (like Ruby) where you modify or extend existing code at runtime without changing the original source files. The name comes from the idea of "monkey-wrenching" your way into existing code—it's a quick and sometimes crude fix.

Key Characteristics

The Problem

The jekyll-jolt gem has a method called prop? that checks if a string matches a certain pattern. However, it doesn't handle integer values properly—it assumes all input is a string. When an integer is passed, the method crashes because integers don't respond to the regex matching operation.

The Solution: The Monkey Patch

Create a file _plugins/jekyll_jolt_fix.rb. Jekyll automatically loads all .rb files in the _plugins/ directory. When this file loads, Ruby reopens the Jekyll::Tags::TemplateBlock class and replaces the buggy prop? method with our fixed version. From that point forward, any call to prop? uses our improved implementation.


comments powered by Disqus