【发布时间】:2016-08-02 21:02:31
【问题描述】:
构建 now_serving 方法,该方法应该调用(即放置)排队的下一个人,然后从前面移除他们。如果没有人排队,它应该喊出(放置)“没有人在等待服务!”。当我尝试使用 shift 方法去除数组中的第一个元素时。我最终得到错误的输出。这是 Ruby 代码:
def now_serving(array)
while array.length != 0
array.each do |name|
puts "Currently serving #{name}."
array.shift
end
end
puts "There is nobody waiting to be served!"
end
但是 array.shift 只工作一次,我如何让它连续删除数组的第一个元素。这是 Rspec 的代码:
describe "#now_serving" do
context "there are no people in line" do
it "should say that the line is empty" do
expect($stdout).to receive(:puts).with("There is nobody waiting to be served!")
now_serving(katz_deli)
end
end
context "there are people in line" do
it "should serve the first person in line and remove them from the queue" do
expect($stdout).to receive(:puts).with("Currently serving Logan.")
now_serving(other_deli)
expect(other_deli).to eq(%w(Avi Spencer))
end
end
end
end
【问题讨论】:
标签: ruby ruby-on-rails-4 rspec