DEV Community

Lucas Barret
Lucas Barret

Posted on

2

Hot Reloading in Ruby

Developer eXperience or DX is really important. Something that we use every day and almost forget is Hot Reload; in Ruby World, there is a cool gem that is appreciated: Spring for Hot Server Reload. So, in this article, we will see how to build a simple preloader in Ruby.

Polling

There are several techniques to achieve what we want. But the easiest one stays polling. Basically, what we will do here is create a watcher that polls over the files of our current directory. Then, our app will reload the files if necessary.

  class Watcher 

    def initialize(files)
      @relative_mtime = files.map do |file|
        File.mtime(file)
      end.max
    end

    def watch
      mtime_max = Dir.glob("**/*").map do |file|
        File.mtime(file)
      end.max

      if @relative_mtime < mtime_max
        @relative_mtime = mtime_max
        true
      else
        false
      end
    end
  end
Enter fullscreen mode Exit fullscreen mode

App/Server Reloading

Now we can build our app, this app is simple but it could be a Rails app.

Here, you see that at the beginning, we require the Watcher, then a test1 file, and a test2 file. But with a little subtle thing. the first one is done with require and the other one with load.

We are going to see why after.

require 'watcher.rb'
require 'test1'
load 'test2.rb'

files = Dir.glob("**/*")

watcher = Watcher.new(files)

while 1
  Kernel.sleep 1
  begin
    Test1.test
    Test2.test
  rescue => exception
    p exception
  end
  stale = watcher.watch
  if stale
    Dir.glob("**/*").each { |f| load(f) }
  end
end
Enter fullscreen mode Exit fullscreen mode

So now, if you change the test2 method, you will see the difference. But if you do so for test1, it won't be reloaded. Using require makes it impossible for you to reload this particular file.

This will also work with autoload, but you must remove the constant first.

Conclusion

Now you know how to make your own hot reload. And, of course, you can find the most effective way to do it.

The Spring Gem has 2 strategies if I am not mistaken. First one the simplest is Polling, otherwise, it uses the listen gem developed by Thibaut Guillaume Gentil.

[Webinar] Building a self-service infrastructure portal

[Webinar] Building a self-service infrastructure portal

Register for the August 6 webinar where we'll be deploying infrastructure with Spacelift in the flavor of Minecraft servers.

Register Now

Top comments (0)

Feature flag article image

Create a feature flag in your IDE in 5 minutes with LaunchDarkly’s MCP server 🏁

How to create, evaluate, and modify flags from within your IDE or AI client using natural language with LaunchDarkly's new MCP server. Follow along with this tutorial for step by step instructions.

Read full post

👋 Kindness is contagious

Take a moment to explore this thoughtful article, beloved by the supportive DEV Community. Coders of every background are invited to share and elevate our collective know-how.

A heartfelt "thank you" can brighten someone's day—leave your appreciation below!

On DEV, sharing knowledge smooths our journey and tightens our community bonds. Enjoyed this? A quick thank you to the author is hugely appreciated.

Okay