在python中,要动态加载模块,您只需使用_____import_____语句并将模块分配给变量I.e(来自文档):
spam = __import__('spam', globals(), locals(), [], -1)
我在python中多次使用这个来模拟动态模块加载/卸载,因为要“卸载”模块,你可以简单地删除对它的所有引用,即:
spam = None
在Ruby中是否有相同的功能?我查看了其他几个问题(this,this和this),但我想知道一种方法将加载的模块约束到变量,如果可能的话。
答案 0 :(得分:3)
这样做你想要的吗?
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
答案 1 :(得分:1)
AFAIK,Ruby实际上没有require
d文件可以分配给变量的单个导出对象的概念;因此,我不知道你会怎么做。
但请注意,您仍然可以使用remove_const
之类的内容来取消定义已加载的类。
答案 2 :(得分:0)
require
或load
一个文件,则将该文件导入全局命名空间。