0

Testing my class source code with some require code and I keep getting the following error:

"D:/ruby193/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in 'require':cannot load such file -- ./xxx.rb (LoadError) from D:/Ruby193/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in 'require' from xxx.rb:1:in ''

Here is the code I'm using to test my code:

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"

Not really educated in ruby on rails errors yet as I'm a student still learning the basics of programming.

Thanks alot for any feedback

# 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
3
  • 2
    Why require "./proj6colecio.rb.txt"? Maybe require "./proj6colecio.rb" Commented Nov 5, 2012 at 23:11
  • ya I took the .txt off just had it to read the file without text editor at school Commented Nov 5, 2012 at 23:12
  • Just a few suggestions (unrelated to the problem): Use attr_reader :color instead of your explicit getter. If you have getters and setters, just use attr_accessor :color. See this page for more details. Your class can be much, much shorter! You might consider using a hash for your constructor too, so you don't have to remember the order of arguments either (e.g., def initialize(opts={}); @color = opts.fetch(:color); end). Commented Nov 6, 2012 at 6:09

2 Answers 2

1

Try require_relative:

require_relative "proj6colecio.rb.txt"

Also, you don't need a .txt file extension for a Ruby script either.

Sign up to request clarification or add additional context in comments.

Comments

1

That syntax just won't work, try require File.join( File.dirname( __FILE__ ), '..', 'proj6colecio.rb' ), instead.

14 Comments

tried that still not working here in the code i'm trying to require. editing post to show full code
You also have an extra end. Take off the last one.
It might help you to use a good editor, with syntax highlighting and formatting cues. I personally like RubyMine a lot.
Finally, ruby error messages are usually pretty good, once you can identify what you need from the stack trace - the reason i knew you had an extra line is this line unexpected keyword_end, expecting $end (SyntaxError) from the console.
@BradWerth: You can only do require "file-in-same-dir" if the current directory is in your $LOAD_PATH, which isn't the case in 1.9. Either doing $LOAD_PATH.unshift File.dirname(__FILE__) before require or simply using require_relative should do the trick.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.