为什么我会收到“LoadError”?

时间:2022-08-22 23:17:41

I have two Ruby scripts, and I am calling those files and I am getting an error.

我有两个Ruby脚本,我正在调用这些文件,我收到一个错误。

I have r1.rb and r2.rb. When I call r1.rb and r2.rb from r3.rb I get this error:

我有r1.rb和r2.rb.当我从r3.rb调用r1.rb和r2.rb时,我收到此错误:

C:/Ruby193/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require': cannot load such file -- r1 (LoadError)
        from C:/Ruby193/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
        from r3.rb:1:in `<main>'

r1.rb is:

def random
    rand(1000000)
end

r2.rb is:

def random
    (rand(26) + 65).chr
end

r3.rb is:

require 'r1'
require 'r2'

puts random

1 个解决方案

#1


-1  

thumb rules when you use require, always use path in require statement [mostly when you use windows platform]

使用require时的拇指规则,总是在require语句中使用路径[主要是在使用Windows平台时]

if you use load you need to place filename.rb but no need to pass file path. [First Check the requirement and use load because it is load every time in memory when call]

如果你使用load你需要放置filename.rb但不需要传递文件路径。 [首先检查需求并使用负载,因为它在呼叫时每次都在内存中加载]

here I am modifying your code

我在这里修改你的代码

require 'C:/PLACE YOUR FILE PATH HERE/r1'
require 'C:/PLACE YOUR FILE PATH HERE/r2'

puts random

Please let me know if this works for you

如果这对你有用,请告诉我

let me add one more things

让我再添加一些东西

Same way when you use irb, you have to do same way

使用irb的方式相同,你必须采用相同的方式

>irb
irb(main):001:0> require 'c:/rubycode/test.rb'
=> true

@jdoe is suggest require_relative that's good approach

@jdoe建议require_relative这是一个很好的方法

but I am getting below error

但我得到了以下错误

irb(main):001:0> require_relative 'test'
LoadError: cannot infer basepath
        from (irb):1:in `require_relative'
        from (irb):1
        from C:/Ruby193/bin/irb:12:in `<main>'

irb(main):002:0> require_relative 'rubycode/test'
LoadError: cannot infer basepath
        from (irb):2:in `require_relative'
        from (irb):2
        from C:/Ruby193/bin/irb:12:in `<main>'

irb(main):003:0> require_relative 'c:/rubycode/test'
LoadError: cannot infer basepath
        from (irb):3:in `require_relative'
        from (irb):3
        from C:/Ruby193/bin/irb:12:in `<main>'

irb(main):004:0> require 'c:/rubycode/test'
=> true
irb(main):005:0>

#1


-1  

thumb rules when you use require, always use path in require statement [mostly when you use windows platform]

使用require时的拇指规则,总是在require语句中使用路径[主要是在使用Windows平台时]

if you use load you need to place filename.rb but no need to pass file path. [First Check the requirement and use load because it is load every time in memory when call]

如果你使用load你需要放置filename.rb但不需要传递文件路径。 [首先检查需求并使用负载,因为它在呼叫时每次都在内存中加载]

here I am modifying your code

我在这里修改你的代码

require 'C:/PLACE YOUR FILE PATH HERE/r1'
require 'C:/PLACE YOUR FILE PATH HERE/r2'

puts random

Please let me know if this works for you

如果这对你有用,请告诉我

let me add one more things

让我再添加一些东西

Same way when you use irb, you have to do same way

使用irb的方式相同,你必须采用相同的方式

>irb
irb(main):001:0> require 'c:/rubycode/test.rb'
=> true

@jdoe is suggest require_relative that's good approach

@jdoe建议require_relative这是一个很好的方法

but I am getting below error

但我得到了以下错误

irb(main):001:0> require_relative 'test'
LoadError: cannot infer basepath
        from (irb):1:in `require_relative'
        from (irb):1
        from C:/Ruby193/bin/irb:12:in `<main>'

irb(main):002:0> require_relative 'rubycode/test'
LoadError: cannot infer basepath
        from (irb):2:in `require_relative'
        from (irb):2
        from C:/Ruby193/bin/irb:12:in `<main>'

irb(main):003:0> require_relative 'c:/rubycode/test'
LoadError: cannot infer basepath
        from (irb):3:in `require_relative'
        from (irb):3
        from C:/Ruby193/bin/irb:12:in `<main>'

irb(main):004:0> require 'c:/rubycode/test'
=> true
irb(main):005:0>