Slide 1

Slide 1 text

Box on Rails RubyKaigi 2026 Follow up Satoshi Tagomori a.k.a., tagomoris - 2026-09-05

Slide 2

Slide 2 text

田籠 聡 (Satoshi Tagomori) @tagomoris SAKURA internet (2024.08〜) Cloud Business Strategy Division Maintainer/Founder: OSS: Ruby, Fluentd, MessagePack, Norikra, Woothee, … Event: ISUCON Service: Pathtraq

Slide 3

Slide 3 text

Asakusa.rb fl https://www. ickr.com/photos/takkanm/3978417669

Slide 4

Slide 4 text

Box on Rails RubyKaigi 2026 Follow up Satoshi Tagomori a.k.a., tagomoris - 2026-09-05

Slide 5

Slide 5 text

This Talk Topics covered • Mastering Ruby Box • RoR in Box • Boxing Tests

Slide 6

Slide 6 text

Mastering Ruby Box

Slide 7

Slide 7 text

Types of Ruby Box on Ruby 4.0 Box#root? Root Box TRUE Box#main? FALSE For method of built-in classes/modules (including RubyGems, etc) Main Box FALSE TRUE Default box, for the main .rb script (and les loaded from the main) Optional Box FALSE FALSE The boxes created by Ruby::Box.new fi User Box

Slide 8

Slide 8 text

Box Timeline in Ruby Process Process start Main start Root Box Box.new Box.new Root Box Main Box Box #3 Box #4

Slide 9

Slide 9 text

Box Timeline in Ruby Process Process start Main start Box.new Box.new Root Box Root Box Main Box Copy from the root box Box #3 Copy from the root box Box #4 Copy from the root box

Slide 10

Slide 10 text

Mutable Root Box: Required in Root Process start Main start Box.new Box.new require ʻjsonʼ Root Box Root Box JSON Main Box Copy from the root box Box #3 Copy from the root box JSON Box #4 Copy from the root box ff The mutable root box makes di erence between boxes

Slide 11

Slide 11 text

Mutable Root Box: States in Root Process start Main start Root Box require ʻrubygemsʼ Gem Box.new Gem Box.new Gem YAML Root Box Main Box require ʻyamlʼ Box #3 require ʻyamlʼ Box #4 Gem has states as class instance variables (e.g., @loaded_specs), and it causes inconsistencies

Slide 12

Slide 12 text

Problems of Mutable Root in 4.0 Root: Source of copies, Box to run built-ins • Source of box copies • Should be immutable • Should NOT run any code (after main starts) • Separated mutable states • Should be loaded per-box

Slide 13

Slide 13 text

In master and 4.0.7 (or later)

Slide 14

Slide 14 text

In master: Master-copy of Ruby Boxes

Slide 15

Slide 15 text

In master: Mastering Ruby Boxes

Slide 16

Slide 16 text

Types of Ruby Box on Ruby master (and 4.0.7/4.1 or later) Box#master? Box#root? Box#main? Master Box Root Box TRUE FALSE FALSE TRUE FALSE The master copy of boxes FALSE For methods of built-in classes/ modules Main Box FALSE FALSE TRUE Default box, for the main .rb script (and les loaded from the main) Optional Box FALSE FALSE FALSE The boxes created by Ruby::Box.new fi User Box

Slide 17

Slide 17 text

Updated Timeline in Ruby Process Process start Main start Box.new Box.new Master Box Master Box require ʻjsonʼ Root Box JSON Copy from the root box Main Box Copy from the root box Box #3 Box as master-copy and to run built-ins are separated! Master box never run any code after main starts Copy from the root box Box #4 Copy from the root box

Slide 18

Slide 18 text

Mutables Separated Per Box Process start Main start Box.new Box.new Master Box Master Box require ʻrubygemsʼ Gem Copy from the root box require ʻrubygemsʼ Gem Root Box Main Box Copy from the root box require ʻrubygemsʼ Gem Box #3 Copy from the root box Every boxes have their own RubyGems (Gems are not built-in now) require ʻrubygemsʼ Gem Box #4 Copy from the root box

Slide 19

Slide 19 text

Did You Master Ruby Box?

Slide 20

Slide 20 text

RoR in Box

Slide 21

Slide 21 text

Ruby::Box Can Run Rails! https://github.com/joker1007/with̲box

Slide 22

Slide 22 text

Works for RoR in Boxes #1 by tagomoris • The separation of the master box • “[Box] Fix about wrong method invalidation - #17119” https://github.com/ruby/ruby/pull/17119

Slide 23

Slide 23 text

Works for RoR in Boxes #2 by hsbt • “[Bug #22123] Do not consume BUNDLER̲SETUP outside the main box - #18218” https://github.com/ruby/ruby/pull/18218 • “Fix gemspec evaluation under Ruby::Box - #9809” https://github.com/ruby/rubygems/pull/9809 • “Make the gem CLI work under RUBY̲BOX=1 - #9810” https://github.com/ruby/rubygems/pull/9810 • “Defer unloading box-local extension DLLs on Windows - #18509” https://github.com/ruby/ruby/pull/18509' • “Fix autoload's require dispatch under Ruby::Box - #18534” https://github.com/ruby/ruby/pull/18534 • “Fix prepend ancestry order in user boxes - #18535” https://github.com/ruby/ruby/pull/18535 • “Copy box-local extensions into a process-private directory - #18536” https://github.com/ruby/ruby/pull/18536

Slide 24

Slide 24 text

Works for RoR in Boxes #3 by niku and hsbt • “Ensure Symbol#to̲proc respects caller's Ruby::Box context - #16865” https://github.com/ruby/ruby/pull/16865 • “Make Symbol#to̲proc respect the caller's box context for user boxes - #18546” https://github.com/ruby/ruby/pull/18546 • “Fix Box resolution crash with IFUNC frames - #16890” https://github.com/ruby/ruby/pull/16890 • “Fix Box resolution crashes with IFUNC frames - #18575” https://github.com/ruby/ruby/pull/18575

Slide 25

Slide 25 text

Works for RoR in Boxes #4 by hsb • “[Bug #22090] Resolve classes in the caller's box in Marshal.load #18544” https://github.com/ruby/ruby/pull/18544 Introduces Primitive.attr! :caller_box to run Ruby code in the master box with the caller box as its context. (This idea is still in discussion)

Slide 26

Slide 26 text

The Released Feature (Ruby::Box) Can Be Tested/Improved!

Slide 27

Slide 27 text

Boxing Tests

Slide 28

Slide 28 text

in Ruby30th 2025-12-30 Testing in boxes Don t you need a funky monkey patches in tests? • Applications tests sometimes need monkey patches • to mock library behaviors, IO objects, … • Most of tests can run without box. Just some of them can t. Ruby Process Application Code Test Runner Test Case Test Case Test Case • How about running tests in boxes? • Running all tests in boxes is too expensive Monkey Patches Test Case Ruby Box ’ ’ • Run just some of them in boxes!

Slide 29

Slide 29 text

Works for Tests in Boxes by tagomoris • “Run .box.rb su x test cases in Ruby Box - #382” https://github.com/test-unit/test-unit/pull/382 Loads test code of les like *.box.rb in separated boxes! Merged, and will be released in the next test-unit version (Available with Ruby 4.1 or later) fi ffi • RSpec? Rake::TestTask?

Slide 30

Slide 30 text

To Be Continued...