数组输出显示在一行上,而不是一行上

时间:2019-04-06 19:49:55

标签: ruby docx

我正在使用docx gem,试图获取与格式匹配的输出,但是我遇到了一些问题。

数组项的输出始终显示在item 1 item 2 item 3这样的一行上

我希望每个输出都显示在一行上,就像这样

item 1
item 2
item 3

在这里输入我的代码

require 'docx'
require 'origami'
require 'nokogiri'
require 'date'
require 'libreconv'



todaysDate = Time.now.strftime("%Y%m%d")
todaysDateSigned = Time.now.strftime("%m/%d/%Y")

doc = Docx::Document.open('Change Request.docx')


puts '----------------------------'

puts 'Enter Client Name'
clientName = gets
clientName = clientName.strip

puts 'Enter Description'
description = gets
description = description.strip

puts 'Enter Hours'
hours = gets
hours = hours.strip

puts 'Enter Effects to other processes'
effects = gets
effects = effects.strip

puts 'process Name'
process = gets
process = process.strip


tasks = Array.new
puts 'Enter Needed Tasks'

loop do
  input = gets.chomp.capitalize
  break if input == ''
  tasks <<  input
end

tasks.join("\n")

tasks.each { |x|  doc.bookmarks['Tasks'].insert_text_after(x + "\n")}




doc.bookmarks['Description'].insert_text_after(description)
doc.bookmarks['Process'].insert_text_after(process)
doc.bookmarks['Date'].insert_text_after(todaysDateSigned)
doc.bookmarks['Date2'].insert_text_after(todaysDateSigned)
doc.bookmarks['Hours'].insert_text_after(hours)
doc.bookmarks['Effects'].insert_text_after(effects)

1 个答案:

答案 0 :(得分:0)

文档显示,使用的方法是^ from the start of the string (.*)\s match and capture anything, up to the last space \( match a literal opening parenthesis (\d+) match and capture the year (any number of digits) \) match a literal closing parenthesis $ end of string ,可以从数组中的每个条目创建多行,但是正确的方法称为insert_multiple_lines_after

我更改了此内容

insert_multiple_lines

对此:

loop do
  input = gets.chomp.capitalize
  break if input == ''
  tasks <<  input
end

tasks.join("\n")

tasks.each { |x|  doc.bookmarks['Tasks'].insert_text_after(x + "\n")}