17

I'm learning Ruby (using version 1.8.6) on Windows 7.

When I try to run the stock_stats.rb program below, I get the following error:

C:\Users\Will\Desktop\ruby>ruby stock_stats.rb stock_stats.rb:1: undefined method `require_relative' for main:Object (NoMethodE rror) 

I have three v.small code files:

stock_stats.rb

require_relative 'csv_reader' reader = CsvReader.new ARGV.each do |csv_file_name| STDERR.puts "Processing #{csv_file_name}" reader.read_in_csv_data(csv_file_name) end puts "Total value = #{reader.total_value_in_stock}" 

csv_reader.rb

require 'csv' require_relative 'book_in_stock' class CsvReader def initialize @books_in_stock = [] end def read_in_csv_data(csv_file_name) CSV.foreach(csv_file_name, headers: true) do |row| @books_in_stock << BookInStock.new(row["ISBN"], row["Amount"]) end end # later we'll see how to use inject to sum a collection def total_value_in_stock sum = 0.0 @books_in_stock.each {|book| sum += book.price} sum end def number_of_each_isbn # ... end end 

book_in_stock.rb

require 'csv' require_relative 'book_in_stock' class CsvReader def initialize @books_in_stock = [] end def read_in_csv_data(csv_file_name) CSV.foreach(csv_file_name, headers: true) do |row| @books_in_stock << BookInStock.new(row["ISBN"], row["Amount"]) end end # later we'll see how to use inject to sum a collection def total_value_in_stock sum = 0.0 @books_in_stock.each {|book| sum += book.price} sum end def number_of_each_isbn # ... end end 

Thanks in advance for any help.

1
  • 2
    That's the code from chapter 3 on the Pickaxe book! Commented Feb 14, 2011 at 21:11

2 Answers 2

34

require_relative doesn't exist in your version of Ruby. You could upgrade Ruby, install the backports gem and require 'backports/1.9.1/kernel/require/relative' but the easiest fix will be to change your require to:

require File.join(File.dirname(__FILE__), 'csv_reader') 
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Shanna, really appreciate your advice
suggest to use require File.expand_path('another_file', __FILE__) which can nicely resolve parent directory if you need to load '../csv_reader'
12

Edit:

Back in the days where this question was asked it referred to Ruby 1.8.6 where there was no require_relative. By now Ruby 1.8.6 is outdated and shouldn't be used anymore.

Original:

There is simply no method name require_relative. You can use require there aswell.

The require_relative function is included in an extension project to the Ruby core libraries, found here: http://www.rubyforge.org/projects/extensions

You should be able to install them with gem install extensions. Then in your code add the following line before the require_relative:

require 'extensions/all' 

3 Comments

There is, but it's in 1.9 only.
@molf: I was only looking for the specified version.
Thanks very much, I've installed the newer version of Ruby and it works.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.