車の今後のサービスの必要性についてユーザーを更新するために、車の走行距離とサービス履歴を追跡するプログラムを作成しました。
Car
、、、の 3 つのクラスがCarHistory
ありCarServiceHistoryEntry
ます。3 つ目は簡単です。サービスに関連付けられたすべての属性 (日付、走行距離、実行されたサービスなど) を保持します。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」での Andy Thomas の議論のように) を採用して、aCarServiceHistoryEntry
を追加できるかどうかをテストしますか?String
セットアップしCarServiceHistoryEntry
て単体テストに追加する代わりに、 a を渡すことはできませんか? - a の必要な属性は配列とメソッドだけなので、
CarHistory
このentries
クラスto_s
をまとめて破棄してクラスに入れるべきcar
でしょうか?