2016-08-02 63 views
0

所以我目前遇到以下錯誤,當我運行我的代碼。我在另一個函數裏面使用了一個函數,我認爲這是問題的根源,但是它已經被指令要求這樣做了。conference_badges #printer應放置徽章和room_assignments的列表

提示:請記住,方法可以調用其他方法。如果assign_rooms的返回值是一組空間分配,那麼如何打印每個分配?您需要遍歷您的房間分配數組,以便分配每個單獨的分配。

Failures: 
 

 
    1) conference_badges #printer should puts the list of badges and room_assignments 
 
    Failure/Error: expect($stdout).to receive(:puts).with(line.chomp) 
 
     
 
     (#<IO:<STDOUT>>).puts("Hello, my name is Edsger.") 
 
      expected: 1 time with arguments: ("Hello, my name is Edsger.") 
 
      received: 0 times 
 
    # ./spec/conference_badges_spec.rb:98:in `block (4 levels) in <top (required)>' 
 

 
Finished in 0.02707 seconds (files took 0.28753 seconds to load) 
 
4 examples, 1 failure

當我運行下面的代碼是給:

def badge_maker(name) 
 
    return "Hello, my name is #{name}." 
 
end 
 

 
def batch_badge_creator(names) 
 
    greetings = [] # initialize greetings as an empty array 
 
    names.each do |name| # for each name in the names array 
 
    greetings << badge_maker(name)# add a greeting for that name 
 
    end 
 
    return greetings # return the array of all greetings, at the end 
 
end 
 

 
def assign_rooms(speakers) 
 
    greet = [] 
 
    speakers.each_with_index{ |speakers, index| greet << "Hello, #{speakers}! You'll be assigned to room #{index+1}!"} 
 
    return greet 
 
    end 
 

 
def printer(inputOne) 
 
    batch_badge_creator(inputOne) 
 
    assign_rooms(inputOne) 
 

 
end

但我不明白爲什麼不匹配輸出來自Rspec:

# Question 4 
 
    # The method `printer` should output first the results of the batch_badge_creator method and then of the assign_rooms method to the screen - this way you can output 
 
    # the badges and room assignments one at a time. 
 
    # To make this test pass, make sure you are iterating through your badges and room assignments lists. 
 

 

 
    it 'should puts the list of badges and room_assignments' do 
 
     badges_and_room_assignments.each_line do |line| 
 
     # $stdout is a Ruby global varibale that represents the current standard output. 
 
     # In this case, the standard output is your terminal screen. This test, then, 
 
     # is checking to see whether or not your terminal screen receives the correct 
 
     # printed output. 
 
     expect($stdout).to receive(:puts).with(line.chomp) 
 
     end 
 
     printer(attendees) 
 
    end 
 

 
    end 
 

 
end

回答

0

這個固定它:

def badge_maker(name) 
 
    return "Hello, my name is #{name}." 
 
end 
 

 
def batch_badge_creator(names) 
 
    greetings = [] # initialize greetings as an empty array 
 
    names.each do |name| # for each name in the names array 
 
    greetings << badge_maker(name)# add a greeting for that name 
 
    end 
 
    return greetings # return the array of all greetings, at the end 
 
end 
 

 
def assign_rooms(speakers) 
 
    greet = [] 
 
    speakers.each_with_index{ |speakers, index| greet << "Hello, #{speakers}! You'll be assigned to room #{index+1}!"} 
 
    return greet 
 
    end 
 

 
def printer(attendees) 
 
    resultOne = batch_badge_creator(attendees) 
 
    resultOne.each do |x| 
 
    puts x 
 
    end 
 
    result = assign_rooms(attendees) 
 
    result.each do |x| 
 
    puts x 
 
    end 
 
end