Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Do Ruby::Box dream of Modular Monolith?

Do Ruby::Box dream of Modular Monolith?

Rubykaigi 2026 LT

Avatar for Tomohiro Hashidate

Tomohiro Hashidate

April 22, 2026

More Decks by Tomohiro Hashidate

Other Decks in Technology

Transcript

  1. I tried to use Ruby::Box on Ruby on Rails. I

    want to separate the domain logic by Ruby::Box. To achieve a fully independent modular monolith. 7
  2. Good parts (?) of Ruby::Box Ruby::Box instances can freely reference

    one another. In other words, a class in one box can be inherited by a class in another box. Of course, you can also prepend or include modules in another box. see. Ruby::Box ダイジェスト紹介(Ruby 4.0.0 新機能) - STORES Product Blog 11
  3. How it works There are 2 boxes. module BoxA def

    foo puts "BoxA" + Ruby::Box.current.inspect super end end module BoxB def foo = puts "BoxB" + Ruby::Box.current.inspect end 12
  4. A = Ruby::Box.new; A.require_relative "box_a" B = Ruby::Box.new; B.require_relative "box_b"

    class Foo prepend A::BoxA include B::BoxB def foo puts "Main" + Ruby::Box.current.inspect super end end Foo.new.foo # => returns: BoxA#<Ruby::Box:3,user,optional> Main#<Ruby::Box:2,user,main> BoxB#<Ruby::Box:4,user,optional> 13
  5. Just by calling super you can seamlessly switch between the

    Box. dangerous (interesting)!! By the way, tagomoris-san said. 14
  6. Applying to Rails A controller within a box inherits ApplicationController

    in the main box. You can evaluate controller and model logic within the child box, And once the controller logic is complete, transparently transfer process to the controller running in the main box. Render views in the main box. Views invoke model method, the logic works on the child box. 15
  7. 16

  8. What you need to get started For now, we need

    to apply a patch to zeitwerk we need to override Ruby::Box#require to work automatic module generation. Otherwise, if you run it with RUBY_BOX=1 , it can't load rails components. This issue is related. https://bugs.ruby-lang.org/issues/21830 18
  9. Actually, this demo app crashes with a SEGV quite often

    when it starts up. I recommend ruby-4.0.2 to run this sample. 21
  10. Things I want related to Ruby::Box require doesn't crash Ruby::Box#name

    : Box#inspect is confusing. I want to rename it to make it clearer. Ruby::Box#fork : I want to create a box while retaining the LOADED_FEATURE that has already been required in the base box. 22
  11. Difficulties How to coordinate zeitwerk. When you cross between boxes,

    it becomes unclear which box the autoload require is being executed in. 23
  12. Conclusion Currently, it is possible to run Ruby::Box on Rails.

    However, it’s very minimal and very experimental. Still, the way Box works is really interesting!! I can sense its potential!! Let's try to use Ruby::Box and play around with it!! 24