开发者

Why I can't load scripts with specific filenames with `load`?

开发者 https://www.devze.com 2023-01-24 17:45 出处:网络
Yesterday I discovered, that I can\'t reload my script rss.rb using load. It only do something unknown for me for several seconds, returns true and doesn\'t 开发者_运维问答execute script. But It can b

Yesterday I discovered, that I can't reload my script rss.rb using load. It only do something unknown for me for several seconds, returns true and doesn't 开发者_运维问答execute script. But It can be loaded (once) with no problems using require_relative. I had to rename script file, to use it as I wanted.

So why require_relative works, but load doesn't? And how to know, which filenames can't be loaded with load?

I'm using ruby 1.9.2p0 (2010-08-18) [i386-mingw32]

UPD:

C:> type 1.rb
p load 'rss.rb'
p load '2.rb'

C:> type rss.rb p 'rss.rb loaded'

C:> type 2.rb p '2.rb loaded'

C:> ruby 1.rb true "2.rb loaded" true


You're loading the rss.rb from the standard library. load goes through the $LOAD_PATH first and only if the file is not found there, looks for the file in the current directory.

You can make it look only in the current directory by doing load "./rss.rb".

The reason it works with require_relative is that require_relative never looks at the $LOAD_PATH.

When using load you should keep in mind that load, unlike require_relative will look for the file in the current directory, not in the directory where the script is. So whether or not it finds the file depends on which directory you're in when invoking the script. (Of course the same was true for loading local files using require in previous ruby versions).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号