所以我在运行代码时遇到以下错误。我在另一个函数中使用了一个函数,我认为它是问题的根源,但是它已经按照说明的方式被要求了。
提示:请记住,方法可以调用其他方法。如果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 :(得分: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
&#13;