Arrays 使用带有索引的每个_的Ruby

Arrays 使用带有索引的每个_的Ruby,arrays,ruby,each,puts,Arrays,Ruby,Each,Puts,我希望使用此方法在名称数组katz_deli中的每个项目中循环,并使用put显示名称及其索引。但是,输出只是数组及其索引中的第一个名称 我的代码: 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

我希望使用此方法在名称数组
katz_deli
中的每个项目中循环,并使用
put
显示名称及其索引。但是,输出只是数组及其索引中的第一个名称

我的代码:

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
我希望我的输出是
“当前行是:1.Logan 2.Avi 3.Spencer”

但是我得到了
“目前的线路是:1.Logan。”
谢谢

您可以首先构建输出字符串,并在准备好后将其放入

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
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)
input=[“Logan”、“Avi”、“Spencer”]
def管路(卡兹杜德利)
如果katz_deli.count>1
output=“该行当前为:”
katz_deli.每个带有索引do的|名称、索引|

输出您可以首先构建输出字符串,并在准备好后将其放入

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)
input=[“Logan”、“Avi”、“Spencer”]
def管路(卡兹杜德利)
如果katz_deli.count>1
output=“该行当前为:”
katz_deli.每个带有索引do的|名称、索引|

输出您确定数组中有多个元素吗?如果是,请提供阵列数据的示例。我无法再现您的问题。当我调用
line([“Logan”、“Avi”、“Spencer”])
时,我得到三行输出:
当前行为:1。Logan
该行当前为:2。Avi
,并且
该行当前为:3。Spencer
。数组是katz_deli=[“Logan”、“Avi”、“Spencer”]。我懂了。有没有一种方法可以让这一切都集中在一行上——也就是说,输出正好是
,当前这行是:1。洛根2号。阿维3。斯宾塞
我这样做是为了一门课程,我想这就是导师们想要的。谢谢您确定数组中有多个元素吗?如果是,请提供阵列数据的示例。我无法再现您的问题。当我调用
line([“Logan”、“Avi”、“Spencer”])
时,我得到三行输出:
当前行为:1。Logan
该行当前为:2。Avi
,并且
该行当前为:3。Spencer
。数组是katz_deli=[“Logan”、“Avi”、“Spencer”]。我懂了。有没有一种方法可以让这一切都集中在一行上——也就是说,输出正好是
,当前这行是:1。洛根2号。阿维3。斯宾塞
我这样做是为了一门课程,我想这就是导师们想要的。谢谢