I am retrieving emails using the Fetcher plugin for Rails. It is doing a fine job. But I am trying to split the body of the email on newlines but it appears that it is only one really long line.
我正在使用Rails的Fetcher插件检索电子邮件。它做得很好。但我试图在新行上拆分电子邮件的正文,但看起来它只是一个很长的行。
What is the best way (in Ruby) to split an email up into multiple lines?
将电子邮件分成多行的最佳方式(在Ruby中)是什么?
1 个解决方案
#1
Sounds like you need a word wrapping algorithm. Here is a short and clever way of word wrapping in Ruby that I found on the ruby-talk mailing list (link is to Google's cache because the site seems to be down):
听起来你需要一个自动换行算法。这是Ruby中一个简短而巧妙的自动换行方式,我在ruby-talk邮件列表中找到了(链接是谷歌的缓存,因为该网站似乎已关闭):
puts $<.read.gsub(/\t/," ").gsub(/.{1,50}(?:\s|\Z)/){($& +
5.chr).gsub(/\n\005/,"\n").gsub(/\005/,"\n")}
Here's a slightly prettier version wrapped in a method:
这是一个包含在方法中的稍微漂亮的版本:
def wordwrap(str, columns=80)
str.gsub(/\t/, " ").gsub(/.{1,#{ columns }}(?:\s|\Z)/) do
($& + 5.chr).gsub(/\n\005/, "\n").gsub(/\005/, "\n")
end
end
#1
Sounds like you need a word wrapping algorithm. Here is a short and clever way of word wrapping in Ruby that I found on the ruby-talk mailing list (link is to Google's cache because the site seems to be down):
听起来你需要一个自动换行算法。这是Ruby中一个简短而巧妙的自动换行方式,我在ruby-talk邮件列表中找到了(链接是谷歌的缓存,因为该网站似乎已关闭):
puts $<.read.gsub(/\t/," ").gsub(/.{1,50}(?:\s|\Z)/){($& +
5.chr).gsub(/\n\005/,"\n").gsub(/\005/,"\n")}
Here's a slightly prettier version wrapped in a method:
这是一个包含在方法中的稍微漂亮的版本:
def wordwrap(str, columns=80)
str.gsub(/\t/, " ").gsub(/.{1,#{ columns }}(?:\s|\Z)/) do
($& + 5.chr).gsub(/\n\005/, "\n").gsub(/\005/, "\n")
end
end