2

In python, to dynamically load a module, you can simply use the _____import_____ statement and assign the module to a variable, I.e(from the docs):

spam = __import__('spam', globals(), locals(), [], -1)

I have used this several times in python in order to simulate dynamic module loading/unloading, because to "unload" the module, you can simply remove all references to it, I.e:

spam = None

Is there an equivalent to this in Ruby? I looked at a few other questions (this, this, and this), but I wanted to know a way to constrain a loaded module to a variable, if possible.

Community
  • 1
  • 1
WilHall
  • 11,644
  • 6
  • 31
  • 53

3 Answers3

3

Does this do what you want?

require 'bigdecimal/math' # a module from stdlib
bm = BigMath # a module is just an object
BigMath = nil # yields a warning, but BigMath is gone.
puts bm.log(10, 40).to_s # it's alter ego lives.
#=> 0.230258509299404568401799145468436420760110148862877297632502494462371208E1 
steenslag
  • 79,051
  • 16
  • 138
  • 171
  • After doing some testing, the method you proposed seems to work the best, but I don't like it. It doesn't seem like good practice, even when using `remove_const` to clean up after yourself. I don't like the fact that Ruby neglects to treat an imported file as its own object like Python does (because doing so makes sense, IMHO). True, many other languages don't let you do this any more than Ruby does, it seems. But I think I'll consider redesign instead of doing something like this - it just feels too hackish. – WilHall Dec 11 '11 at 15:45
  • It's not particularly hackish. The module defines an object, then you can do with the object what you like. – Marnen Laibow-Koser Dec 11 '11 at 22:36
  • Eh, I feel like the extra step is a sign that it's not meant to be done in the first place. So I'll just avoid it. – WilHall Dec 12 '11 at 21:55
  • FWIW, I believe the Rails autoloader uses `remove_const` to force reloading of changed classes when running in development mode. But yeah, in most cases, there shouldn't be anything to clean up after. Ruby gems and libraries are usually pretty good about not polluting the global namespace, whereas an exports object would probably be too much constraint on what a module can do. – Marnen Laibow-Koser Dec 13 '11 at 18:27
1

AFAIK, Ruby doesn't really have the concept of a single export object that a required file could assign to a variable; thus, I don't see how you would do this.

Note, however, that you could still use things like remove_const to undefine classes that have already been loaded.

Marnen Laibow-Koser
  • 5,959
  • 1
  • 28
  • 33
0

Nah, not possible. If you require or load a file in Ruby, you import the file into the global namespace.

Reactormonk
  • 21,472
  • 14
  • 74
  • 123
  • actually, `load` does not load the code in the global namespace if you use the second parameter "wrap", which will execute the code inside an anonymous module. But since there is no way (I know of) to access this anonymous module there is little difference. Also, globals will still pollute the global namespace. – riffraff Dec 09 '11 at 19:04