将目录添加到 $LOAD_PATH (Ruby) [英] Adding a directory to $LOAD_PATH (Ruby)

查看:39
本文介绍了将目录添加到 $LOAD_PATH (Ruby)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我见过两种常用的技术,用于将当前正在执行的文件的目录添加到 $LOAD_PATH(或 $:).我看到了这样做的好处,以防您不使用 gem.显然,一个似乎比另一个更冗长,但有理由选择一个吗?

I have seen two commonly used techniques for adding the directory of the file currently being executed to the $LOAD_PATH (or $:). I see the advantages of doing this in case you're not working with a gem. One seems more verbose than the other, obviously, but is there a reason to go with one over the other?

第一个冗长的方法(可能有点矫枉过正):

The first, verbose method (could be overkill):

$LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__))) unless $LOAD_PATH.include?(File.expand_path(File.dirname(__FILE__)))

以及更直接、快速和肮脏的:

and the more straightforward, quick-and-dirty:

$:.unshift File.dirname(__FILE__)

有什么理由选择一个?

推荐答案

我会说使用 $:.unshift File.dirname(__FILE__) 而不是另一个,因为我已经看到它在代码中的使用比 $LOAD_PATH 多得多,而且它也更短!

I would say go with $:.unshift File.dirname(__FILE__) over the other one, simply because I've seen much more usage of it in code than the $LOAD_PATH one, and it's shorter too!

这篇关于将目录添加到 $LOAD_PATH (Ruby)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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