我在python中有一个简洁的小脚本,我想移植到Ruby,我认为它突出了我在Ruby上的无趣性。我收到的错误是有一个意外的END语句,但我不知道这是怎么回事。也许有一个关键字需要一个END或者不想要我忘记的END的东西。以下是导致违规行违规行的所有代码。
begin
require base64
require base32
rescue LoadError
puts "etext requires base32. use 'gem install --remote base32' and try again"
end
# Get a string from a text file from disk
filename = ARGV.first
textFile = File.open(filename)
text = textFile.read()
mailType = "text only" # set the default mailType
#cut the email up by sections
textList1 = text.split(/\n\n/)
header = textList1[0]
if header.match (/MIME-Version/)
mailType = "MIME"
end
#If mail has no attachments, parse as text-only. This is the class that does this
class TextOnlyMailParser
def initialize(textList)
a = 1
body = ""
header = textList[0]
@parsedEmail = Email.new(header)
while a < textList.count
body += ('\n' + textList[a] + '\n')
a += 1
end
@parsedEmail.body = body
end
end
def separate(text,boundary = nil)
# returns list of strings and lists containing all of the parts of the email
if !boundary #look in the email for "boundary= X"
text.scan(/(?<=boundary=).*/) do |bound|
textList = recursiveSplit(text,bound)
end
return textList
end
if boundary
textList = recursiveSplit(text,boundary)
end
end
def recursiveSplit(chunk,boundary)
if chunk.is_a? String
searchString = "--" + boundary
ar = cunk.split(searchString)
return ar
elsif chunk.is_a? Array
chunk do |bit|
recursiveSplit(bit,boundary);
end
end
end
class MIMEParser
def initialize(textList)
@textList = textList
@nestedItems = []
newItem = NestItem.new(self)
newItem.value = @textList[0]
newItem.contentType = "Header"
@nestedItems.push(newItem)
#setup parsed email
@parsedEmail = Email.new(newItem.value)
self._constructNest
end
def checkForContentSpecial(item)
match = item.value.match (/Content-Disposition: attachment/)
if match
filename = item.value.match (/(?<=filename=").+(?=")/)
encoding = item.value.match (/(?<=Content-Transfer-Encoding: ).+/)
data = item.value.match (/(?<=\n\n).*(?=(\n--)|(--))/m)
dataGroup = data.split(/\n/)
dataString = ''
i = 0
while i < dataGroup.count
dataString += dataGroup[i]
i ++
end #<-----THIS IS THE OFFENDING LINE
@parsedEmail.attachments.push(Attachment.new(filename,encoding,dataString))
end
答案 0 :(得分:2)
你的问题是i ++
行,Ruby没有post或pre递增/递减运算符,并且该行无法解析。我无法亲自说明为什么i++
在IRB中进行评估,但i ++
不会执行任何操作。
而是将++
运算符替换为+= 1
,使其成为最后while
:
while i < dataGroup.count
dataString += dataGroup[i]
i += 1
end
但是也要考虑ruby方式,如果你只是将它添加到一个字符串中,为什么不用dataString = dataGroup.join
而不是用while结构循环呢?