Require ruby file without .rb extension?

2019-02-16 08:59发布

I have a ruby file that does not have a .rb extension, and is instead identified as ruby code with a shebang at the beginning of the file: #!/usr/bin/env ruby. I want to require the code in this file in another ruby file, but it seems to be having a problem because require automatically appends the .rb extension to the files it looks for. Is there any way to suppress this behavior and make require only look for the file as the name is given?

2条回答
别忘想泡老子
2楼-- · 2019-02-16 09:21

We have scripts the users are allowed to call, which are accessed using symlinks. The symlinks exist in our /usr/local/bin directory, which is automatically included in their path via a minor adjustment to /etc/profile.

Our real scripts exist in a separate directory using the normal Ruby name, ending with ".rb". We can access those using require if necessary.

/usr/local/bin/foo --> /usr/local/share/app_name/foo.rb
查看更多
男人必须洒脱
3楼-- · 2019-02-16 09:25

Use load instead:

load 'file-name'
查看更多
登录 后发表回答