Ruby loaderror无法加载此类文件

时间:2012-11-05 23:05:08

标签: ruby-on-rails ruby require

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

  

“d:/ruby193/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:在   'require':无法加载此类文件 - ./xxx.rb(LoadError)   d:/Ruby193/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:在   来自xxx.rb的'require':1:在''

以下是我用来测试代码的代码:

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

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

尝试require_relative

require_relative "proj6colecio.rb.txt"

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