4 ms·
That would be a very jarring change. Ruby class definitions are executed to define a class. There is no way to statically define a class in Ruby. This is freque
by vidarh 7y ago
That would be a very jarring change. Ruby class definitions are executed to define a class. There is no way to statically define a class in Ruby. This is frequently used. E.g lots of Ruby libraries add class methods that are executed during the definition of a class to mutate it during definition. This is Ruby's alternative to macros.
As such, you'd gut large parts of the language if you stopped evaluating code on require.
- pdimitar 7y agoYep, but that's a post factum worry. It's not an inherent trait of being a dynamic language that automatically surfaces during its development. Ruby's creator(s) simply chose to do it like that and now, of course, it's too late to do any sort of revolutionary changes. Hi btw! Just now noticed it's you. :) Was thinking to write you an e-mail very soon. Which one do you prefer?
- vidarh 7y agoYes, of course it could have been done differently from the start, but as you say it's now impossible to change. Though my Ruby compiler project (that seems like it will take the rest of my life at this point...) would have been far easier if it was more static. That said, I think it's less of a problem in practice - most code does things that can be fairly easily statically determined and/or that isn't actually doing anything particularly nefarious to other classes. A lot of the worst abuses that are possible with Ruby are also things I've never seen done intentionally in the wild other than as examples, and where deprecating and explicitly breaking them wouldn't do any harm. > Hi btw! Just now noticed it's you. :) Was thinking to write you an e-mail very soon. Which one do you prefer? Sure :) Same old e-mails (also on my HN profile page).
- bernawil 7y agoIsn't the real problem that Ruby's "magic" a lot of the time relies on monkey-patching stuff? I mean, Javascript's modules also are dynamically executed and then returned as a singleton module object (also cached, so only the first require actually runs the code before the module export). This means you can make a module which requires other modules and monkey patches methods inside them (this is how mocking actually works), wrecking havoc amongst all other dependencies, but library authors just don't.
- vidarh 7y agoThe more in-your-face monkey-patching went out of favour many years ago. Most newer Ruby libraries make you explicitly "opt in" to changes by e.g. including a module and/or contain any monkey-patching in separate files that needs to be explicitly require'd if you want that behavior. It's rarely a problem in practice.
- matheusmoreira 7y agoThe code evaluation is not the problem. The fact all evaluation takes place in a single global interpreter context is the problem. When a file that defines a class A is required, the A constant of the Object class gets set to a new instance of Class: # a.rb class A end # irb require 'a' Object::A.new All code evaluation modifies global interpreter state. Honestly it's not much better than the C preprocessor's #include directive. A better design: files are always evaluated with a clean interpreter state and they can return values just like methods. # a.rb class A end A # irb a = require 'a' a.new This makes all state local to each script by default. Users will only have access to the returned value which can contain anything or nothing. If a module needs to modify existing classes, they could return modules that can be mixed in or metaprogramming methods that perform the modifications when called with the targeted class as parameter. Perhaps existing classes could even be passed as parameter: require 'extensions', Object, Kernel Of course, these changes would break compatibility with all existing gems. The result would be a completely different language that happens to also be called Ruby.
- vidarh 7y agoMost of the code that changes global interpreter state does so in predictable ways that are perfectly fine, so I don't think what you're suggesting would actually make much change. Most modern Ruby code avoids monkey-patching "unasked" unless that is an explicit purpose of the project and using the gem is explicitly "asking for it" in the first place. Providing modules to include or making you explicitly call methods or explicitly require files documented to do optional monkey-patching has been the idiomatic way of handling it for years. Adding the ability to explicitly provide guarantees that the required code is sandboxed might be useful and might particularly help tooling reason better about the code, but it would have quite little practical effect on most modern Ruby.