Adding a directory to $LOAD_PATH (Ruby)

2020-01-25 03:16发布

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?

7条回答
Melony?
2楼-- · 2020-01-25 04:21

There is a gem which will let you setup your load path with nicer and cleaner code. Check this out: https://github.com/nayyara-samuel/load-path.

It also has good documentation

查看更多
登录 后发表回答