Ruby:如何从完全独立的文件树中请求文件

时间:2021-02-25 23:20:12

I have two separate paths for some Ruby scripts I want to reference in a Rails application. The first file is Rails.root/lib/assets/myscript.rb, and the second is under Rails.root/resources/repo/lib/myotherscript.rb. How would I reference myotherscript in myscript? I already know about requiring files from relative paths and such, but how would referencing be done from completely separate file trees?

对于我想在Rails应用程序中引用的一些Ruby脚本,我有两个单独的路径。第一个文件是Rails.root / lib / assets / myscript.rb,第二个文件位于Rails.root / resources / repo / lib / myotherscript.rb下。我如何在myscript中引用myotherscript?我已经知道要求相对路径等文件,但如何从完全独立的文件树中完成引用?

1 个解决方案

#1


0  

As Stefan said.
You could just use require_relative to load any file in any file tree.

正如斯特凡所说。您可以使用require_relative来加载任何文件树中的任何文件。

look:

看:

 $ cat /etc/hello.rb 
 module Hello
   def say_hello
     puts "Hello"
   end 
 end

==============================

==============================

 $ cat /Users/amalrik/code/use_hello.rb 
 require_relative '/etc/hello'

 include Hello

 say_hello

==============================

==============================

 $ ruby use_hello.rb 
 Hello

EDIT: Here you can take a look in an example of that in rails context and compare with your solution: https://github.com/amalrik/require_relative_on_rails

编辑:在这里你可以看看rails上下文中的一个例子,并与你的解决方案进行比较:https://github.com/amalrik/require_relative_on_rails

EDIT: I've just realize that require works too if you specify the full path. So i suggest double check your code for typos. Look:

编辑:我刚刚意识到,如果指定完整路径,也需要工作。所以我建议仔细检查你的代码是否有错别字。看:

 $ cat /etc/hello.rb 
 module Hello
   def say_hello
     puts "Hello"
   end 
 end

==============================

==============================

 $ cat /Users/amalrik/code/use_hello.rb 
 require '/etc/hello'

 include Hello

 say_hello

==============================

==============================

 $ ruby use_hello.rb 
 Hello

For a more detail explanation of ruby load path i suggest this read: $: == $LOAD_PATH

有关ruby加载路径的更详细说明,我建议阅读:$:== $ LOAD_PATH

#1


0  

As Stefan said.
You could just use require_relative to load any file in any file tree.

正如斯特凡所说。您可以使用require_relative来加载任何文件树中的任何文件。

look:

看:

 $ cat /etc/hello.rb 
 module Hello
   def say_hello
     puts "Hello"
   end 
 end

==============================

==============================

 $ cat /Users/amalrik/code/use_hello.rb 
 require_relative '/etc/hello'

 include Hello

 say_hello

==============================

==============================

 $ ruby use_hello.rb 
 Hello

EDIT: Here you can take a look in an example of that in rails context and compare with your solution: https://github.com/amalrik/require_relative_on_rails

编辑:在这里你可以看看rails上下文中的一个例子,并与你的解决方案进行比较:https://github.com/amalrik/require_relative_on_rails

EDIT: I've just realize that require works too if you specify the full path. So i suggest double check your code for typos. Look:

编辑:我刚刚意识到,如果指定完整路径,也需要工作。所以我建议仔细检查你的代码是否有错别字。看:

 $ cat /etc/hello.rb 
 module Hello
   def say_hello
     puts "Hello"
   end 
 end

==============================

==============================

 $ cat /Users/amalrik/code/use_hello.rb 
 require '/etc/hello'

 include Hello

 say_hello

==============================

==============================

 $ ruby use_hello.rb 
 Hello

For a more detail explanation of ruby load path i suggest this read: $: == $LOAD_PATH

有关ruby加载路径的更详细说明,我建议阅读:$:== $ LOAD_PATH