2016-08-02 4 views
0

次の人を呼び出して(つまりputする)now_servingメソッドをビルドして、フロントから削除する必要があります。もし誰も並んでいなければ、「奉仕するのを待っている人はいない!」と叫ぶべきです。シフトメソッドを使用して配列の最初の要素を取り除こうとすると、私は間違った出力に終わる。ここではRubyのコードは次のとおりです。FIFO 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

答えて

0

それを介して、同時にあなたのループを配列をシフトすることは悪い考えです。

def now_serving(array) 
    while name = array.shift 
    puts "Currently serving #{name}." 
    end 

    puts "There is nobody waiting to be served!" 
end 
+0

私のバグを修正。 '' '失敗/エラー:now_serving(other_deli) #>受信:予期しない引数を指定して置きます 予想:( "現在、ローガンサービスを提供しています。") は得た:( "現在、アビサービスを提供しています。") - –

+0

@chumakoff何ですか'katz_deli'と' other_deli'ですか?これらはどこから来たのですか? – chumakoff

+0

これはRspecのテストです。 @chumakoff –

0

これは、私は、コードを実行したときに、私は次のエラーを取得しています

def now_serving(array) 
 
    queue = Queue.new 
 
    queue = array 
 
    
 
    if array.length > 0 
 
    puts "Currently serving #{array[0]}." 
 
    array.shift 
 
    else 
 
    puts "There is nobody waiting to be served!" 
 
    end 
 
end

関連する問題