Ruby 中的 require_relative 和 require 有什么区别? [英] What is the difference between require_relative and require in Ruby?

查看:44
本文介绍了Ruby 中的 require_relative 和 require 有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

require_relativerequire 在 Ruby 中有什么区别?

解决方案

看看文档:

<块引用>

require_relative 对内置方法 require 进行了补充,允许您加载与包含 require_relative 语句的文件相关的文件.>

例如,如果您在test"目录中有单元测试类,而在测试test/data"目录下有它们的数据,那么您可以在测试用例中使用这样的一行:

require_relative "data/customer_data_1"

What is the difference between require_relative and require in Ruby?

解决方案

Just look at the docs:

require_relative complements the builtin method require by allowing you to load a file that is relative to the file containing the require_relative statement.

For example, if you have unit test classes in the "test" directory, and data for them under the test "test/data" directory, then you might use a line like this in a test case:

require_relative "data/customer_data_1"

这篇关于Ruby 中的 require_relative 和 require 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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