在 ruby​​ 中,我如何知道哪个模块被定义为“load”的结果? 或“需要”?

发布于 2024-07-11 05:44:28 字数 543 浏览 5 评论 0原文

在 ruby​​ 中,如果我“require foo”,有没有办法随后确定 foo.rb 中定义的一个或多个模块的名称?

例如,假设我有一个名为 foo.rb 的 ruby​​ 文件,如下所示:

# foo.rb
module MyModule
    def self.summary
        "this does something useful"
    end
    ...
end

在另一个脚本中,在我执行“require foo”之后,如何确定我现在有一个名为 MyModule 的模块?

我最终想要的是能够做这样的事情:

file = someComputedFileName()
require file
puts "summary of #{file}: #{???::summary}

虽然我可以强迫自己使模块名称和文件名相同,但我宁愿不这样做。 我想要更自由地制作短文件名但更具表现力的模块名称。 但是,我向自己保证每个文件只会定义一个模块(想想:插件)。

In ruby, if I do "require foo", is there a way to subsequently determine the name of the module or modules defined in foo.rb?

For example, say I have a ruby file named foo.rb that looks like this:

# foo.rb
module MyModule
    def self.summary
        "this does something useful"
    end
    ...
end

In another script, after I do "require foo", how can I determine that I now have a module named MyModule?

Ultimate what I'm after is to be able to do something like this:

file = someComputedFileName()
require file
puts "summary of #{file}: #{???::summary}

While I could force myself to make module names and file names identical, I'd rather not. I want a bit more freedom in making short filenames but more expressive module names. However, I am guaranteeing to myself that each file will only define a single module (think: plugins).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

度的依靠╰つ 2024-07-18 05:44:28

我不知道这是否是最好的解决方案,但从我的角度来看,这似乎可行:

all_constants = Object.constants
require 'foo'

foo_constants = Object.constants - all_constants

foo_constants 应该只为您提供由 foo.rb 定义的模块、类或其他常量。

I don't know if this is the best solution, but off the top of my head this seems to work:

all_constants = Object.constants
require 'foo'

foo_constants = Object.constants - all_constants

foo_constants should give you only the modules, classes or other constants that were defined by foo.rb.

故笙诉离歌 2024-07-18 05:44:28

一种方法是使用 ObjectSpace.each_object(Module) 来查找在需要该文件之前定义的所有模块。 然后,在需要该文件后,再次循环所有定义的模块,看看是否有新的模块。

require "set"
old_modules = SortedSet.new
ObjectSpace.each_object(Module) {|m| old_modules.add(m) }

file = someComputedFileName()
require file

new_modules = SortedSet.new
ObjectSpace.each_object(Module) {|m| new_modules.add(m) unless old_modules.include?(m) }

puts "summary of #{file}: #{new_modules.to_a.map{|m|m.summary}.join(',')}"

这还可以让您在文件中定义多个模块。

One approach would be to use ObjectSpace.each_object(Module) to find all defined modules before requiring the file. Then, after you require the file, loop over all defined modules again and see if there are any new ones.

require "set"
old_modules = SortedSet.new
ObjectSpace.each_object(Module) {|m| old_modules.add(m) }

file = someComputedFileName()
require file

new_modules = SortedSet.new
ObjectSpace.each_object(Module) {|m| new_modules.add(m) unless old_modules.include?(m) }

puts "summary of #{file}: #{new_modules.to_a.map{|m|m.summary}.join(',')}"

That would also let you define more than one module in the file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文