1

I have a neat little script in python that I would like to port to Ruby and I think it's highlighting my noobishness at Ruby. I'm getting the error that there is an unexpected END statement, but I don't see how this can be so. Perhaps there is a keyword that requires an END or something that doesn't want an END that I forgot about. Here is all of the code leading up to the offending line Offending line is commented.

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

1 Answer 1

2

Your issue is the i ++ line, Ruby does not have a post or pre increment/decrement operators and the line is failing to parse. I can't personally account as to why i++ evaluates in IRB but i ++ does not perform any action.

Instead replace your ++ operators with += 1 making that last while:

while i < dataGroup.count
  dataString += dataGroup[i]
  i += 1
end

But also think about the ruby way, if you're just adding that to a string why not do a dataString = dataGroup.join instead of looping over with a while construct?

Sign up to request clarification or add additional context in comments.

4 Comments

Ruby allows whitespace between an operator and its operand. i++ \n j is perfectly valid and is the same as i + +j which is the same as i.+(+j) which is the same as i.+(j.+@()). Therefore, i++ on a line by itself could be valid if the next line were something like j. IRb can't yell at you for just writing i++ because you could very well be supplying the missing operand on the next line.
@izuriel I didn't do it the Ruby way because I'm just now learning Ruby. Thank you for the tip and advice and also the solution solved the problem
@JörgWMittag Okay, that makes sense. Thank you for giving me the "why" behind this question
@JörgWMittag I'm quite aware of that fact, the statement I made was that since ruby does not define (nor accurately parse) increment operators, I was surprised to find that in IRB i++ evaluated properly as increment but i ++ did not (both in the same context). White space is not a major factor in most languages and i ++; is just as valid as i++ but it fails to parse in Ruby.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.