Beginner to RUBY - require_relative problem

Posted by WANNABE on Stack Overflow See other posts from Stack Overflow or by WANNABE
Published on 2010-04-21T07:28:17Z Indexed on 2010/04/21 7:33 UTC
Read the original article Hit count: 585

Filed under:
|

Hi, 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.

© Stack Overflow or respective owner

Related posts about ruby

Related posts about beginner