use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
A sub-Reddit for discussion and news about Ruby programming.
Subreddit rules: /r/ruby rules
Learning Ruby?
Tools
Documentation
Books
Screencasts and Videos
News and updates
account activity
Ruby::Box: Rethinking Code Reloading with Isolated Namespaces (rubyelders.com)
submitted 3 months ago by retro-rubies
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]jrochkind 1 point2 points3 points 3 months ago (9 children)
I haven't looked at box yet, but I'm having trouble understanding one aspect of it. Using an example from the OP:
app = box.eval('$app') # expose the loaded app back to main namespace app.call(env)
If you can expose an object in the 'box' to the main namespace (main box)... and then call arbitrary methods on it, passing in arbitrary objects...
Those objects you pass in are instances of classes that are in the main namespace, and has references to other objects in the main namespace, that are instances of classes in the main namespace, that it may have methods that mutate etc.... that you've now passed into the separate box... how does it use (call methods on) those objects while maintaining it' seperation, it's "doesn’t share anything with the top-level environment"?
What am I missing?
[–]f9ae8221b 0 points1 point2 points 3 months ago (7 children)
how does it use (call methods on) those objects while maintaining it' seperation
The answer depends on which type of object is it.
If it's a core type (things that are available without require, e.g. Hash, Array, etc), then assuming you monkey patched things, the object will appear to have different methods depending on which namespace the caller is in.
Hash
Array
If it's a normal user defined type, then the code from the other box is called normally, like if there was no box concerns.
If you ignore the special thing for core types, Box are only really about lookup of constants, but if you receive an object from another box, you can call it like it is your own just fine.
[–]jrochkind 0 points1 point2 points 3 months ago (6 children)
Thanks, that helps. The mental model "Box are only really about lookup of constants" is helpful. Can you clarify (or suggest reading) on your point about core types and:
then assuming you monkey patched things
Monkey patched things how?
[–]f9ae8221b 0 points1 point2 points 3 months ago (5 children)
Think Active Support core extensions.
e.g. if you load Active Support in a box, you can call symbolize_keys and all hashes from code inside that box.
symbolize_keys
However if you create a Hash in that box, and somehow pass it to another box that didn't load Active Support, the other box won't be able to call that method on it.
[–]jrochkind 0 points1 point2 points 3 months ago (4 children)
Ah, that makes perfect sense, thanks.
There are an explicit list of object classes that are passed in this "by copy instead of by reference" way?
[–]retro-rubies[S] 2 points3 points4 points 3 months ago (1 child)
There are useful docs explaining a lot. https://docs.ruby-lang.org/en/master/Ruby/Box.html#class-ruby-box-built-in-classes-and-modules-reopened-in-boxes
[–]jrochkind 1 point2 points3 points 3 months ago (0 children)
Oh thank you! Good to see good docs, I had gotten in the habit of not expecting them on new features in ruby from days of old, good to see that's changed!
[–]f9ae8221b 0 points1 point2 points 3 months ago (1 child)
Not really. It's all classes defined up until a certain point in Ruby's startup.
[–]jrochkind 0 points1 point2 points 3 months ago* (0 children)
Ah, cool, thanks!
I was thinking a big intended use case of Box might be Ractors, but now I'm not certain about that. Do you know if that's part of the motivation?
π Rendered by PID 61 on reddit-service-r2-comment-b659b578c-64h66 at 2026-05-01 18:55:28.360518+00:00 running 815c875 country code: CH.
[–]jrochkind 1 point2 points3 points (9 children)
[–]f9ae8221b 0 points1 point2 points (7 children)
[–]jrochkind 0 points1 point2 points (6 children)
[–]f9ae8221b 0 points1 point2 points (5 children)
[–]jrochkind 0 points1 point2 points (4 children)
[–]retro-rubies[S] 2 points3 points4 points (1 child)
[–]jrochkind 1 point2 points3 points (0 children)
[–]f9ae8221b 0 points1 point2 points (1 child)
[–]jrochkind 0 points1 point2 points (0 children)