LoadError on terminal中的'./primes.rb'

时间:2013-05-07 18:41:42

标签: ruby terminal require

当我在irb中要求'./primes.rb'时,我得到了这个:

  1.9.3-p392 :004 > require './primes.rb'
LoadError: cannot load such file -- ./primes.rb
    from /Users/RBonhardt/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
    from /Users/RBonhardt/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
    from (irb):4
    from /Users/RBonhardt/.rvm/rubies/ruby-1.9.3-p392/bin/irb:16:in `<main>'

这是primes.rb文件:

# primes.rb
require 'debugger'

def prime?(num)
debugger
  (1..num).each do |i|
    if (num % i) == 0
      return false
    end
  end
end

def primes(num_primes)
  ps = []
  num = 1
  while ps.count < num_primes
    primes << num if prime?(num)
  end
end

if __FILE__ == $PROGRAM_NAME
  puts primes(100)
end

非常感谢任何有关如何使其发挥作用的建议!

当我确实需要亲戚时,它会给我这个:

1.9.3-p392 :010 > require_relative 'primes.rb'
LoadError: cannot infer basepath
    from (irb):10:in `require_relative'
    from (irb):10
    from /Users/RBonhardt/.rvm/rubies/ruby-1.9.3-p392/bin/irb:16:in `<main>'

当我在下面做第二个解决方案时,它给了我这个:

1.9.3-p392 :013 > $LOAD_PATH << "."
 => ["/Users/RBonhardt/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/site_ruby/1.9.1", "/Users/RBonhardt/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/site_ruby/1.9.1/x86_64-darwin11.4.2", "/Users/RBonhardt/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/site_ruby", "/Users/RBonhardt/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/vendor_ruby/1.9.1", "/Users/RBonhardt/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/vendor_ruby/1.9.1/x86_64-darwin11.4.2", "/Users/RBonhardt/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/vendor_ruby", "/Users/RBonhardt/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1", "/Users/RBonhardt/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/x86_64-darwin11.4.2", "."] 
1.9.3-p392 :014 > require 'primes.rb'
LoadError: cannot load such file -- primes.rb
    from /Users/RBonhardt/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
    from /Users/RBonhardt/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
    from (irb):14
    from /Users/RBonhardt/.rvm/rubies/ruby-1.9.3-p392/bin/irb:16:in `<main>'
1.9.3-p392 :015 > 

当我在pry中尝试时:

[4] pry(main)> require_relative 'primes.rb'
LoadError: cannot infer basepath
from (pry):2:in `require_relative'
[5] pry(main)> require 'primes.rb'
LoadError: cannot load such file -- primes.rb
from /Users/RBonhardt/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
[6] pry(main)> .ls
Applications        Movies          git-completion.bash
Desktop         Music           rails_projects
Documents       Pictures        ruby
Downloads       Public          runwithfriends
Dropbox         code            shopify
Library         dev         sites
[7] pry(main)> require 'ruby/app_acad_mini_curriculum/debugging/primes.rb'
LoadError: cannot load such file -- ruby/app_acad_mini_curriculum/debugging/primes.rb
from /Users/RBonhardt/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'

2 个答案:

答案 0 :(得分:0)

尝试require_relative

require_relative 'primes.rb'

修改 请注意,这只能在脚本中使用。如果您尝试require此脚本进入irb会话,则需要提供primes.rb的完整路径。原因在于irb的位置。例如,在irb中尝试Dir.pwd,您会看到require_relative尝试搜索primes.rb的位置。

你可以做几件事:

# Just need to require the one file.
require_relative File.join('users', 'yourusername', 'prime_folder', 'prime.rb')

# Many files in the same folder
$LOAD_PATH << File.join('users', 'yourusername', 'prime_folder')
require 'prime.rb'
require 'another_file.rb'

我使用的另一个选项是Pry。它就像irb,很容易从脚本调用。这是一颗宝石:

gem install pry

在脚本结束时,您可以执行以下操作:

if $0 == __FILE__
  require 'pry'
  binding.pry
end

然后你会进入像REPL这样的irb,在那里你可以测试和调试你的方法。没有它,我无法生存。

答案 1 :(得分:0)

与ruby 1.8不同,您不能要求同一文件夹中的文件,因为当前文件夹不再位于加载路径上。

要模仿ruby 1.8的行为,您可以尝试

$LOAD_PATH << "."
require 'primes.rb'

然而,正如@CharlesCaldwell指出的那样,正确使用ruby 1.9的方法是使用relative_require。

这是解决此问题的最佳方式的good discussion

请注意relative_require 在irb 中不起作用。您可以查看@CharlesCaldwell答案的动机。

但是看看你的任务问题,你不应该使用irb,你应该使用pry:

  

我们将使用两颗宝石。一个叫做Pry,它是irb的替代品。你必须宝石安装撬。调试使用Pry并不是必需的,但它会让生活变得更好。

以下是使用相对要求的示例:

[fotanus@thing ~]$ cat primes.rb 
# primes.rb

def prime?(num)
  (1..num).each do |i|
    if (num % i) == 0
      return false
    end
  end
end

def primes(num_primes)
  ps = []
  num = 1
  while ps.count < num_primes
    primes << num if prime?(num)
  end
end

if __FILE__ == $PROGRAM_NAME
  puts primes(100)
end
[fotanus@thing ~]$ cat a.rb 
require_relative 'primes.rb'
[fotanus@thing ~]$ ruby a.rb