是否需要宝石加载一切,包括我不使用的东西? [英] Does requiring a gem load everything, including things I don't use?

查看:134
本文介绍了是否需要宝石加载一切,包括我不使用的东西?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设 x 是一个gem,它包含 Hello Goodbye require'x',但只使用类。

code> Hello class。是否加载了 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屋!

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