It would be great if we somehow had a way to load a module-as-file without unknown side-effects, and without depending so deeply on the other contents of the global namespace.
But this is basically describing a complete overhaul of most of what makes ruby ruby, so... ¯\_(ツ)_/¯
class A
B = "c".freeze
end
And elsewhere: class String
def freeze
raise "because I can, that's why"
end
# or even method_added, TracePoint, ...
end
It feels like something should be possible here, but it's really steep uphill battle.You could say it was a separate language .rb-module or something, to make it formal.
I presume you mean "expressions that aren't method definitions in class bodies" (though that's a problem, because of things like attribute declarations) rather than "expressions in method bodies", since methods with no expressions in their bodies would be pointless.
But if you have source code that you know you will likely be requiring such as the standard library, you can do the initial parsing while compiling the Ruby VM, so you don't end up doing the parse twice at runtime.
Long term what we hope to do is to provide a build of the Ruby VM that includes the version of Rails you are using pre-parsed.
And then longer term we'd like to actually fully parse and initialise Rails (run the top level of the files which are loaded) during compilation, and freeze the heap and store it in the Ruby VM executable. When you run this special Ruby/Rails VM the Rails code is simply mmapped into your address space with all objects initialised and ready to go.
Obviously it'll require some tweaking to delay doing this like starting the web server so that doesn't get run during compile time.
It's a really interesting idea to pre-load the heap with just a set of libraries, which wouldn't be subject to as much change.