构建新客户在进入熟食店时将使用的方法。 take_a_number方法应该接受两个参数,即当前行的数组(katz_deli),以及包含希望加入该行的人的名字的字符串。该方法应该返回该人的姓名及其在线的位置。如何排列以使当前行的数组等于names数组的数量?
katz_deli = []
def line(array)
if array[0] == nil
puts "The line is currently empty."
end
end
def take_a_number(array, name)
i=0
counter = 1
while array.count != name.length
array[i] = counter
i+=1
counter +=1
end
if array.count > 1
puts "The line is currently:"
end
name.each_with_index {|val, index| puts "#{index+1}. #{val}"}
end

这是Rspec文件:
describe 'Deli Counter' do
let(:katz_deli) { [] }
let(:other_deli) { ["Logan", "Avi", "Spencer"] }
describe "#line" do
context "there is nobody in line" do
it "should say the line is empty" do
# This line checks the current standard output (your terminal screen)
# to make sure the correct output has been puts'ed.
expect($stdout).to receive(:puts).with("The line is currently empty.")
line(katz_deli)
end
end
context "there are people in line" do
it "should display the current line" do
expect($stdout).to receive(:puts).with("The line is currently: 1. Logan 2. Avi 3. Spencer")
line(other_deli)
end
end
end

答案 0 :(得分:0)
我提前做错了方法
katz_deli = []
def line(array)
if array.count > 1
output = "The line is currently:"
array.each_with_index {|val, index| output << " #{index+1}. #{val}"}
puts output
else
puts "The line is currently empty."
end
end
&#13;