我希望这种方法遍历名称....
url(r'^users/$', view=UsersView.as_view(), name='users_index'),
url(r'^users/(?P<user_id>\d+)/$', view=UsersView.as_view(), name='users_user'),
...
数组中的每个项目,并使用katz_deli
来显示名称及其索引。但是,输出只是数组中带有索引的第一个名称。
我的代码:
puts
我希望我的输出为def line (katz_deli)
if katz_deli.count > 1
katz_deli.each_with_index {|name, index| puts "The line is currently: #{index +1}. #{name}" }
else
puts "The line is currently empty."
end
end
但我得到"The line is currently: 1. Logan 2. Avi 3. Spencer"
谢谢!
答案 0 :(得分:3)
您可以首先构建输出字符串,并在准备就绪后构建puts
:
input = ["Logan", "Avi", "Spencer"]
def line (katz_deli)
if katz_deli.count > 1
output = "The line is currently:"
katz_deli.each_with_index do |name, index|
output << " #{index +1}. #{name}"
end
puts output
else
puts "The line is currently empty."
end
end
line(input)
答案 1 :(得分:1)
def line (katz_deli)
if katz_deli.count > 1
print "The line is currently:"
katz_deli.each_with_index {|name, index| print " #{index +1}. #{name}" }
else
puts "The line is currently empty."
end
end