Slide 1

Slide 1 text

Do Ruby::Box dream of Modular Monolith? joker1007 (Repro株式会社) Rubykaigi 2026 LT 1

Slide 2

Slide 2 text

self.inspect 橋立友宏 (@joker1007) Repro inc. Chief Architect I love , , and Karaoke 2

Slide 3

Slide 3 text

Gold Sponsor 3

Slide 4

Slide 4 text

I come from Asakusa(.rb) 4

Slide 5

Slide 5 text

Ruby::Box comes!! It's very interesting feature!! 5

Slide 6

Slide 6 text

Have you touched Ruby::Box? 6

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

And It works!! (minimal and not practical) However, it required a few workarounds. 8

Slide 9

Slide 9 text

Demo time!! 9

Slide 10

Slide 10 text

Yeah!! The class space is separated for each URL!! 10

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

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# Main# BoxB# 13

Slide 14

Slide 14 text

Just by calling super you can seamlessly switch between the Box. dangerous (interesting)!! By the way, tagomoris-san said. 14

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

16

Slide 17

Slide 17 text

Thank you tagomoris-san!! Ruby::Box is fun!! 17

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

By the way, can't we use Ruby::Box with Rails::Engine? 19

Slide 20

Slide 20 text

It crashes for now require fails to execute in the first place. 20

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

Difficulties How to coordinate zeitwerk. When you cross between boxes, it becomes unclear which box the autoload require is being executed in. 23

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

For more details at the 松江Ruby会議. Stay Tuned!! 25