I'm trying to print several arrays on one line. The first line is ok but the loop breaks down after that. I've tried going over it serveral times but no cigar. Here's the code:
我正在尝试在一行上打印几个数组。第一行是正常的,但循环在此之后崩溃。我已经尝试了几次,但没有雪茄。这是代码:
chapter_num = ['Chapter 1', 'Chapter 2', 'Chapter 3']
chapter_title = ['Numbers', 'Letters', 'Variables']
chapter_page = ['page 1', 'page 72', 'page 118']
#Prints chapter number
chapter_num.each do |num|
print (num.ljust 0)
#Prints title
chapter_title.each do |title|
print (title.center 14)
#Prints page number
chapter_page.each do |page|
puts (page.rjust 40)
end
end
end
When I run the above code it looks like this:
当我运行上面的代码时,它看起来像这样:
Chapter 1 Numbers page 1
page 72
page 118
Letters page 1
page 72
page 118
Variables page 1
page 72
page 118
Chapter 2 Numbers page 1
page 72
page 118
Letters page 1
page 72
page 118
Variables page 1
page 72
page 118
Chapter 3 Numbers page 1
page 72
page 118
Letters page 1
page 72
page 118
Variables page 1
page 72
page 118
It's suppose to look like this:
它假设看起来像这样:
Chapter 1 Numbers page 1
Chapter 2 Letters page 72
Chapter 3 Variables page 118
2 个解决方案
#1
2
You could use the Array::zip
method here:
你可以在这里使用Array :: zip方法:
chapter_num = ['Chapter 1', 'Chapter 2', 'Chapter 3']
chapter_title = ['Numbers', 'Letters', 'Variables']
chapter_page = ['page 1', 'page 72', 'page 118']
arr = chapter_num.zip(chapter_title, chapter_page)
arr.each { |el| puts "#{el[0].ljust 0} #{el[1].center 14} #{el[2].rjust 40}" }
#=> Chapter 1 Numbers page 1
#=> Chapter 2 Letters page 72
#=> Chapter 3 Variables page 118
And--since Array::zip
takes an optional block--you could do it in one line:
并且 - 由于Array :: zip采用可选块 - 您可以在一行中执行:
chapter_num.zip(chapter_title, chapter_page).each { |el| puts "#{el[0].ljust 0} #{el[1].center 14} #{el[2].rjust 40}" }
#2
1
In your code you have three nested loops which is the reason for getting the output you are getting. You only need to have one loop for printing the i-th element of each of your arrays like this:
在你的代码中,你有三个嵌套循环,这是获得你得到的输出的原因。你只需要有一个循环来打印每个数组的第i个元素,如下所示:
chapter_num = ['Chapter 1', 'Chapter 2', 'Chapter 3']
chapter_title = ['Numbers', 'Letters', 'Variables']
chapter_page = ['page 1', 'page 72', 'page 118']
#Prints chapter number
(0..chapter_num.length-1).each do |i|
puts "#{chapter_num[i].ljust 0} #{chapter_title[i].center 14} #{chapter_page[i].rjust 40}"
end
which prints out:
打印出来:
Chapter 1 Numbers page 1
Chapter 2 Letters page 72
Chapter 3 Variables page 118
#1
2
You could use the Array::zip
method here:
你可以在这里使用Array :: zip方法:
chapter_num = ['Chapter 1', 'Chapter 2', 'Chapter 3']
chapter_title = ['Numbers', 'Letters', 'Variables']
chapter_page = ['page 1', 'page 72', 'page 118']
arr = chapter_num.zip(chapter_title, chapter_page)
arr.each { |el| puts "#{el[0].ljust 0} #{el[1].center 14} #{el[2].rjust 40}" }
#=> Chapter 1 Numbers page 1
#=> Chapter 2 Letters page 72
#=> Chapter 3 Variables page 118
And--since Array::zip
takes an optional block--you could do it in one line:
并且 - 由于Array :: zip采用可选块 - 您可以在一行中执行:
chapter_num.zip(chapter_title, chapter_page).each { |el| puts "#{el[0].ljust 0} #{el[1].center 14} #{el[2].rjust 40}" }
#2
1
In your code you have three nested loops which is the reason for getting the output you are getting. You only need to have one loop for printing the i-th element of each of your arrays like this:
在你的代码中,你有三个嵌套循环,这是获得你得到的输出的原因。你只需要有一个循环来打印每个数组的第i个元素,如下所示:
chapter_num = ['Chapter 1', 'Chapter 2', 'Chapter 3']
chapter_title = ['Numbers', 'Letters', 'Variables']
chapter_page = ['page 1', 'page 72', 'page 118']
#Prints chapter number
(0..chapter_num.length-1).each do |i|
puts "#{chapter_num[i].ljust 0} #{chapter_title[i].center 14} #{chapter_page[i].rjust 40}"
end
which prints out:
打印出来:
Chapter 1 Numbers page 1
Chapter 2 Letters page 72
Chapter 3 Variables page 118