从其他位置运行ruby文件时加载错误

时间:2021-12-23 01:50:18

I have the following problem:

我有以下问题:

My ruby project structure : Ruby_Source\ file1.rb file2.rb file3.rb

我的ruby项目结构:Ruby_Source \ file1.rb file2.rb file3.rb

In file1.rb, require 'file2' require 'file3'

在file1.rb中,要求'file2'需要'file3'

now ,if I run the file1.rb from Ruby_Source, am not getting any error.

现在,如果我从Ruby_Source运行file1.rb,我没有收到任何错误。

but , when I run the same from a different system location eg(c:)

但是,当我从不同的系统位置运行相同的时候,例如(c :)

error is Load error.

错误是加载错误。

Can some one help me please?

有人能帮助我吗?

2 个解决方案

#1


3  

You might want to use require_relative:

您可能想要使用require_relative:

require_relative complements the builtin method require by allowing you to load a file that is relative to the file containing the require_relative statement.

require_relative通过允许您加载与包含require_relative语句的文件相关的文件来补充内置方法require。

See further discussion:

见进一步讨论:

What is the difference between require_relative and require in Ruby?

Ruby中的require_relative和require有什么区别?

And if you run Ruby 1.8:

如果你运行Ruby 1.8:

Ruby: require vs require_relative - best practice to workaround running in both Ruby <1.9.2 and >=1.9.2

Ruby:require vs require_relative - 在Ruby <1.9.2和> = 1.9.2中运行的解决方法的最佳实践

#2


0  

Try this:

require_relative 'file2'

in Ruby 1.9.x. It will search for file2 in the directory of file1.

在Ruby 1.9.x.它将在file1目录中搜索file2。

In older versions you might try something like:

在旧版本中,您可以尝试以下方法:

$: << File.dirname($0) 

which will add the current program's path to the require-search path.

这将把当前程序的路径添加到require-search路径。

#1


3  

You might want to use require_relative:

您可能想要使用require_relative:

require_relative complements the builtin method require by allowing you to load a file that is relative to the file containing the require_relative statement.

require_relative通过允许您加载与包含require_relative语句的文件相关的文件来补充内置方法require。

See further discussion:

见进一步讨论:

What is the difference between require_relative and require in Ruby?

Ruby中的require_relative和require有什么区别?

And if you run Ruby 1.8:

如果你运行Ruby 1.8:

Ruby: require vs require_relative - best practice to workaround running in both Ruby <1.9.2 and >=1.9.2

Ruby:require vs require_relative - 在Ruby <1.9.2和> = 1.9.2中运行的解决方法的最佳实践

#2


0  

Try this:

require_relative 'file2'

in Ruby 1.9.x. It will search for file2 in the directory of file1.

在Ruby 1.9.x.它将在file1目录中搜索file2。

In older versions you might try something like:

在旧版本中,您可以尝试以下方法:

$: << File.dirname($0) 

which will add the current program's path to the require-search path.

这将把当前程序的路径添加到require-search路径。