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

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

问题描述

我见过两种常用的技术,用于将当前正在执行的文件的目录添加到$ LOAD_PATH(或$ :)。如果你不使用宝石,我会看到这样做的好处。其中一个看起来比另一个更详细,但是是否有理由与另一个进行比较?第一个详细方法(可能是过度杀伤): / p>

  $ LOAD_PATH.unshift(File.expand_path(File.dirname(__ FILE__)))除非$ LOAD_PATH.include?(File.expand_path (File.dirname(__ FILE__)))

以及更直接,快速和肮脏:

  $ :. unshift File.dirname(__ FILE__)

有什么理由与其中一个去?

解决方案

我会说请在 $ :. unshift File.dirname(__ FILE __)之上使用另一个,因为我已经在代码中看到了比 $ LOAD_PATH 一个,它也短一些!


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__)

Any reason to go with one over the other?

解决方案

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天全站免登陆