简体   繁体   English

Ruby - 将目录添加到$ LOAD_PATH - 它有什么作用?

[英]Ruby - adding a directory to $LOAD_PATH - what does it do?

This is actually a question about this question: Adding a directory to $LOAD_PATH (Ruby) 这实际上是一个关于这个问题的问题: 将目录添加到$ LOAD_PATH(Ruby)

What happens when you add a directory to the $LOAD_PATH? 将目录添加到$ LOAD_PATH会发生什么? Is it like adding a script file to the execution context as in JavaScript? 是否像在JavaScript中那样将脚本文件添加到执行上下文中? (You can access global methods/objects in another files) If not, how do I call the methods of other ruby files in the current directory? (您可以访问其他文件中的全局方法/对象)如果没有,我如何调用当前目录中其他ruby文件的方法?

When you add the /Users/you/scripts/ruby directory to the load path, you can use: /Users/you/scripts/ruby目录添加到加载路径时,可以使用:

require 'example'

instead of: 代替:

require '/Users/you/scripts/ruby/example.rb'

Think of the $LOAD_PATH as to being similar to the PATH variable on a operating system. $LOAD_PATH视为与操作系统上的PATH变量类似。 If certain directories are in the LOAD_PATH, you can just write require "some_module" . 如果某些目录在LOAD_PATH中,您只需编写require "some_module" It's also the reason for being able to require files from the current directory. 这也是能够require当前目录中的文件的原因。

By default, the LOAD_PATH no longer includes the current directory . 默认情况下,LOAD_PATH不再包含当前目录. having been removed in Ruby 1.9.2 . 在Ruby 1.9.2中删除

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM