为什么我会收到此错误
in`':未初始化的常量Order :: ItemContainer(NameError)
-------------------------
class Cart
attr_reader :items
include ItemContainer
def initialize(owner)
@items=Array.new
@owner=owner
end
def save_to_file
File.open("#{@owner}_cart.txt","w") do |f|
@items.each {|i|f.puts "#{i.name}:#{i.price}:#{i.weight}"}# car:100:50
end
end
def read_from_file
return unless File.exists? ("#{@owner}_cart.txt")
item_fields=File.readlines("#{@owner}_cart.txt") #"car:100:50\n"
item_fields.map! {|i|i.chomp} #"car:100:50"
item_fields.map! {|i|i.split(":")} #["car","100","50"]
item_fields.each {|i|@items << Item.new(name: i[0],price: i[1].to_i,weight: i[2].to_i)}
@items.uniq!
end
end
这是我的计划的第二部分
------------------------
module ItemContainer
module ClassMethods
def min_price
100
end
end
module InstanceMethods
def add_item(item)
unless item.price<self.class.min_price
@items.push item
end
end
def remove_item
@items.pop
end
def shift_item
@items.shift
end
def validate
@items.each {|i|puts "Item has no price" if i.price.nil?}
end
def delete_invalid_items
@items.delete_if {|i| i.price.nil?}
end
def count_valid_items
@items.count {|i| i.price}
end
end
def self.included(base)
base.extend ClassMethods
base.class_eval do
include InstanceMethods
end
end
end
我认为回调和模块正常 ----------------------
require_relative "item_container" require_relative "cart"
require_relative "order" require_relative "item" require_relative
"real_item" require_relative "virtual_item"
@items=[]
@items<<VirtualItem.new({:price=>101,:weight=>20,:name=>'car'})
@items<<RealItem.new({:price=>101,:weight=>20,:name=>'kettle'})
@items<<RealItem.new({:price=>101,:weight=>20,:name=>'dishwasher'})