我创建了一个程序来跟踪汽车里程和服务历史,以便更新用户对汽车的未来服务需求。
我有三个类:Car、CarHistory和CarServiceHistoryEntry。第三个是直截了当的;它包含与服务相关的所有属性:日期、里程、执行的服务等。CarHistory类如下:
require_relative 'car_service_history_entry'
class CarHistory
attr_reader :entries
def initialize (*entry)
if entry.size > 1
@entries = []
else
@entries = entry
end
end
def add_service_entry entry
@entries << entry
end
def to_s
entries_string = ""
@entries.each {|entry| entries_string << "#{entry.to_s}\n"}
entries_string
end
end
- 在
initialize中,是否应该entry检查类? - 在
add_service_entry,采用鸭子类型(如安迪托马斯在“编程Ruby”中的论点),我什至会测试是否CarServiceHistoryEntry可以添加a?我不能只通过 aString而不是设置然后添加CarServiceHistoryEntry我的单元测试吗? - 由于 a 的唯一必要属性
CarHistory是entries数组和to_s方法,我是否应该将这个类全部废弃并将其放入car类中?