我有一个递归问题,没有按预期返回,我不确定如何更改函数。
我想要一个包含字符串和数组的数组,这些字符串和数组派生自由文本中定义的边界分隔的一大块文本。第一遍有效,但第二遍似乎没有正确地将包含边界关键字的字符串转换为数组并让它们在父数组中替换自己。
递归函数:
def separate(text,boundary = nil)
# returns array of strings and arrays containing all of the parts of the email
textList = []
if !boundary #look in the email for "boundary= X"
text.scan(/(?<=boundary=).*/) do |bound|
textList = recursiveSplit(text,bound)
end
end
if boundary
textList = recursiveSplit(text,boundary)
end
puts textList.count
puts '\n'
puts textList.inspect
return textList
end
def recursiveSplit(chunk,boundary)
if chunk.is_a? String
searchString = "--" + boundary
ar = chunk.split(searchString)
return ar
elsif chunk.is_a? Array
chunk do |bit|
recursiveSplit(bit,boundary);
end
end
end
文本变量示例:
MIME-Version: 1.0
Received: by 10.112.170.40 with HTTP; Fri, 3 May 2013 05:08:21 -0700 (PDT)
Date: Fri, 3 May 2013 08:08:21 -0400
Delivered-To: me@gmail.com
Message-ID: <CADPp44E47syuXvP1K-aemhcU7vdSijZkfKLu-74QPWs9U9551Q@mail.gmail.com>
Subject: MiB 5/3/13 7:43AM (EST)
From: Me <me@gmail.com>
To: Someone <someone@aol.com>
Content-Type: multipart/mixed; boundary=BNDRY1
--BNDRY1
Content-Type: multipart/alternative; boundary=BNDRY2
--BNDRY2
Content-Type: text/plain; charset=ISO-8859-1
-TEXT STUFF HERE. SAYING THINGS
ABOUT CERTAIN THINGS
--BNDRY2
Content-Type: text/html; charset=ISO-8859-1
HTML FORMATTED TEXT STUFF
--BNDRY2--
--BNDRY1
Content-Type: application/zip; name="Make it Brief.ipa.zip"
Content-Disposition: attachment; filename="Make it Brief.ipa.zip"
Content-Transfer-Encoding: base64
X-Attachment-Id: f_hg9biuno0
<<FILE DATA>>
--BNDRY1--