我正在尝试改进release_bike
方法。我已经进入 irb,第一个保护条件有效,并且 release_working_bikes 工作,但第二个保护条件在 irb 中保持返回 nil,即使我运行功能测试并且知道只有一辆损坏的自行车可用。
我表述第二条失败线的方式有问题,还是有缺陷broken_bikes
?
该release_bike
方法应按以下方式工作;如果扩展坞中没有自行车,则应该有一个警告说 -No bikes available
如果扩展坞中有自行车,但它们都坏了,那么应该有一个警告说 -No working bikes available
如果有一些工作自行车,那么release_bike
应该释放其中一辆 workign 自行车。
以下是涉及的两个类;
require_relative 'bike'
class DockingStation
DEFAULT_CAPACITY = 20
attr_reader :capacity, :bikes
def initialize(capacity = DEFAULT_CAPACITY)
@bikes = []
@capacity = capacity
end
def release_bike
fail 'No bikes available' if empty?
fail 'No working bikes available' unless broken_bikes
release_working_bikes
end
def dock(bike)
fail 'Docking Station Full' if full?
@bikes << bike
end
private
def working_bikes
@bikes.each { |bike| return bike unless bike.broken? }
end
def broken_bikes
not_working = []
not_working << @bikes.each { |bike| return bike if bike.broken? }
not_working.empty?
end
def release_working_bikes
bike = working_bikes
@bikes.delete(bike)
end
def full?
@bikes.count >= @capacity
end
def empty?
@bikes.empty?
end
end
class Bike
attr_accessor :broken
def initialize
@broken = false
end
def working?
@working
end
def report_broken
@broken = true
end
def broken?
@broken
end
end