相当于Python的Ruby动态导入语句? [英] Equivalent to Python's dynamic import statement in Ruby?

查看:107
本文介绍了相当于Python的Ruby动态导入语句?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在python中,要动态加载模块,您可以简单地使用 _____ import _____ 语句并将模块分配给变量,即从文档中获取:

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)

python为了模拟动态模块加载/卸载,因为要卸载模块,可以简单地删除所有的引用,即:

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

Ruby中是否有相当于此?我看了一些其他问题(这个),但是我想知道一种将加载的模块约束到变量的方法,如果可能的话

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.

推荐答案

这是否符合您的要求?

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 

这篇关于相当于Python的Ruby动态导入语句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆