【问题标题】:Ruby load error cannt load such fileRuby loaderror 无法加载此类文件
【发布时间】:2012-10-25 20:41:15
【问题描述】:

用一些需要代码测试我的类源代码,我不断收到以下错误:

"D:/ruby193/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in 'require': 无法加载此类文件 -- ./xxx.rb (LoadError) from D:/Ruby193/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in 来自 xxx.rb:1:in 的“要求”

这是我用来测试我的代码的代码:

require "./proj6colecio.rb.txt"
print " "
guitar = Guitar.new("Stratocaster", "Fender", "Solid Body", 6, "Black")
print "Guitar Name: #{guitar.name}\n"
print "Guitar Brand: #{guitar.brand}\n"
print "Guitar Type: #{guitar.type}\n"
print "Number of Strings: #{guitar.strings}\n"
print "Guitar Color: #{guitar.color}\n"
print guitar, "\n"

还没有真正接受过 ruby​​ on rails 错误方面的教育,因为我是一名仍在学习编程基础知识的学生。

非常感谢您的任何反馈

# Guitar class with instance variables @name, @brand, @type @strings @color and
# method take_strings.

class Guitar

  # initialize method is called when user invokes Guitar.new.
  def initialize(the_name, the_brand, the_type, the_strings, the_color)
    @name = the_name
    @brand = the_brand
    @type = the_type
    @strings = the_strings
    @color = the_color
  end

  # Define getters
  def name
    return @name
  end

  def brand
    return @brand
  end

  def type
    return @type
  end
  def strings
    return @strings
  end

  def color
    return @color
  end
# define setters

  def strings=(value)
    @strings = value
  end

  def to_s
    return "The Guitar is a #{name} made by #{brand}. It is a #{type} with #{strings} strings and is #{color}."
  end

  def change_color
    @color = "Blue"
  end

end

guitars = [ ]

guitars << Guitar.new("Stratocaster", "Fender", "Solid Body", 6, "Black")
guitars << Guitar.new("Les Paul", "Gibson", "Solid Body", 6, "Yellow")
guitars << Guitar.new("White Falcon", "Gretsch", "Semi-Hollow", 6, "White")

# Print all guitars
guitars.each do |g|
  print g, "\n"
end

#Change color of guitar to blue
guitars.each do |g|
  g.change_color
end

guitars.each do |g|
  print g, "\n"
end
end

【问题讨论】:

  • 为什么是require "./proj6colecio.rb.txt"?也许require "./proj6colecio.rb"
  • 是的,我把 .txt 去掉了,只是让它在学校没有文本编辑器的情况下阅读文件
  • 只是一些建议(与问题无关):使用attr_reader :color 而不是您的显式getter。如果您有 getter 和 setter,只需使用 attr_accessor :color。有关详细信息,请参阅this page。你的课可以短得多!您也可以考虑为您的构造函数使用哈希,这样您也不必记住参数的顺序(例如,def initialize(opts={}); @color = opts.fetch(:color); end)。

标签: ruby-on-rails ruby require


【解决方案1】:

该语法不起作用,请尝试使用require File.join( File.dirname( __FILE__ ), '..', 'proj6colecio.rb' )

【讨论】:

  • 尝试在我想要的代码中仍然无法正常工作。编辑帖子以显示完整代码
  • 您还有一个额外的end。取下最后一个。
  • 它可能会帮助您使用一个好的编辑器,带有语法突出显示和格式提示。我个人非常喜欢 RubyMine。
  • 最后,ruby 错误消息通常非常好,一旦您可以从堆栈跟踪中识别出您需要的内容 - 我知道您有额外行的原因是控制台中的这一行 unexpected keyword_end, expecting $end (SyntaxError)
  • @BradWerth:如果当前目录在您的$LOAD_PATH 中,您只能执行require "file-in-same-dir",而在1.9 中并非如此。在require 之前执行$LOAD_PATH.unshift File.dirname(__FILE__) 或简单地使用require_relative 应该可以解决问题。
【解决方案2】:

试试require_relative:

require_relative "proj6colecio.rb.txt"

此外,Ruby 脚本也不需要 .txt 文件扩展名。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-01-22
    • 2012-02-27
    • 2016-04-26
    • 1970-01-01
    • 2016-11-03
    • 2013-08-10
    • 1970-01-01
    • 2012-07-27
    相关资源
    最近更新 更多