视窗7的Im学习R.R.(使用版本1.8.6)。
当我尝试实施以下<>strong>stock_stats.rb方案时,我发现以下错误:
C:UsersWillDesktop
uby>ruby stock_stats.rb
stock_stats.rb:1: undefined method `require_relative for main:Object (NoMethodE
rror)
我有三件“小码”文件:
<>strong>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
事先得到任何帮助。