是否需要宝石加载一切,包括我不使用的东西? [英] Does requiring a gem load everything, including things I don't use?
问题描述
假设 code> Hello x
是一个gem,它包含 Hello
和 Goodbye $如果我编写一个程序
class。是否加载了 require'x'
,但只使用
Goodbye
类?
文件,而不是宝石。
用
require'x'
code>
您加载文件 x.rb
。您加载的 x.rb
由搜索路径定义,搜索路径可以通过gem定义修改(您在示例代码中没有使用的)。 p>
加载文件 x.rb
中的所有内容。如果 x.rb
包含其他 require
命令,那些文件也会被加载。
Assume x
is a gem, that contains both Hello
and Goodbye
classes.
If I write a program that require 'x'
, but only uses the Hello
class. Is the Goodbye
class loaded as well?
You include scripts or files, not gems.
With
require 'x'
you load the file x.rb
. Which x.rb
you load is defined by the search path, the search pathes can be modified by gem definitions (what you didn't use in your example code).
Everything inside the file x.rb
is loaded. If x.rb
contains other require
commands, those files are also loaded.
这篇关于是否需要宝石加载一切,包括我不使用的东西?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!