将输出格式化为每行 40 个字符长

发布于 2024-12-06 13:48:13 字数 523 浏览 4 评论 0原文

我对 Ruby 相当陌生,我已经在 Google 上搜索了几个小时了。 有谁知道如何将打印输出的格式设置为不超过 40 个字符长?

例如:

我想要打印的内容:

This is a simple sentence.
This simple
sentence appears
on four lines. 

但我希望它的格式为:

This is a simple sentence. This simple
sentence appears on four lines.

我将原始数据的每一行放入一个数组中。
so x = ["这是一个简单的句子。", "这个简单", "句子出现", "共三行。"]
我尝试了 x.each { |n| print n[0..40], " " } 但它似乎没有做任何事情。

任何帮助都会很棒!

I'm fairly new to Ruby and I've been searching Google for a few hours now.
Does anyone know how to format the output of a print to be no more than 40 characters long?

For example:

What I want to print:

This is a simple sentence.
This simple
sentence appears
on four lines. 

But I want it formatted as:

This is a simple sentence. This simple
sentence appears on four lines.

I have each line of the original put into an array.
so x = ["This is a simple sentence.", "This simple", "sentence appears", "on three lines."]
I tried x.each { |n| print n[0..40], " " } but it didn't seem to do anything.

Any help would be fantastic!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

ヅ她的身影、若隐若现 2024-12-13 13:48:13

word_wrap 方法需要一个 Strind 并进行一种漂亮的打印。

您的数组将通过 join("\n") 转换为字符串

代码:

def word_wrap(text, line_width = 40 ) 
  return text if line_width <= 0
  text.gsub(/\n/, ' ').gsub(/(.{1,#{line_width}})(\s+|$)/, "\\1\n").strip
end

x = ["This is a simple sentence.", "This simple", "sentence appears", "on three lines."]

puts word_wrap(x.join("\n"))
x << 'a' * 50 #To show what happens with long words
x << 'end'
puts word_wrap(x.join("\n"))

代码说明:

x.join("\n")) 构建一个字符串,然后使用 text.gsub(/\n/, ' ') 构建一长行。
在这种特殊情况下,这两个步骤可以合并:x.join(" "))

现在神奇发生在

gsub(/(.{1,#{line_width}})(\s+|$)/, "\\1\n")
  • (.{1,#{line_width }})):取 line_width 个字符以内的任意字符。
  • (\s+|$):下一个字符必须是空格或行尾(换句话说:如果最后一个字符不是空格,则前一个匹配可能会比 line_width 更短)
  • "\\1\n":取出最多 40 个字符的字符串,并以换行符结束。
  • 完成。

gsub 重复换行直至 最后,我删除前导和尾随空格strip

我还添加了一个长单词(50 个 a),会发生什么情况? gsub 不匹配,单词保持原样。

The method word_wrap expects a Strind and makes a kind of pretty print.

Your array is converted to a string with join("\n")

The code:

def word_wrap(text, line_width = 40 ) 
  return text if line_width <= 0
  text.gsub(/\n/, ' ').gsub(/(.{1,#{line_width}})(\s+|$)/, "\\1\n").strip
end

x = ["This is a simple sentence.", "This simple", "sentence appears", "on three lines."]

puts word_wrap(x.join("\n"))
x << 'a' * 50 #To show what happens with long words
x << 'end'
puts word_wrap(x.join("\n"))

Code explanation:

x.join("\n")) build a string, then build one long line with text.gsub(/\n/, ' ').
In this special case this two steps could be merged: x.join(" "))

And now the magic happens with

gsub(/(.{1,#{line_width}})(\s+|$)/, "\\1\n")
  • (.{1,#{line_width}})): Take any character up to line_width characters.
  • (\s+|$): The next character must be a space or line end (in other words: the previous match may be shorter the line_width if the last character is no space.
  • "\\1\n": Take the up to 40 character long string and finish it with a newline.
  • gsub repeat the wrapping until it is finished.

And in the end, I delete leading and trailing spaces with strip

I added also a long word (50 a's). What happens? The gsub does not match, the word keeps as it is.

吹梦到西洲 2024-12-13 13:48:13
puts x.join(" ").scan(/(.{1,40})(?:\s|$)/m)

这是一个简单的句子。这个简单
句子出现在三行中。

puts x.join(" ").scan(/(.{1,40})(?:\s|$)/m)

This is a simple sentence. This simple
sentence appears on three lines.

呆头 2024-12-13 13:48:13

Ruby 1.9(并且效率不太高):

>> x.join(" ").each_char.each_slice(40).to_a.map(&:join)
=> ["This is a simple sentence. This simple s", "entence appears on three lines."]

您的解决方案不起作用的原因是所有单个字符串都短于 40 个字符,因此 n[0..40] 始终是整个字符串。

Ruby 1.9 (and not overly efficient):

>> x.join(" ").each_char.each_slice(40).to_a.map(&:join)
=> ["This is a simple sentence. This simple s", "entence appears on three lines."]

The reason your solution doesn't work is that all the individual strings are shorter than 40 characters, so n[0..40] always is the entire string.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文