6

I'm new to Ruby and a bit confused by the grep command in this block of code. I'm trying to gather all the mailbox names via Net::IMAP and then check them against a mailbox argument. Likely the mailbox name will only include part of the argument. For example, someone may type in "Sent" as the mailbox, but many times the mailbox name will be "INBOX.Sent."

   class ExamineMail
        def initialize(user, domain, pass, box)
           @username = user
           @domain = domain
           @pass = pass
           @mailbox = box 
        end

         def login()
            @imap = Net::IMAP.new("mail." + @domain)
            @imap.authenticate('LOGIN', @username + "@" + @domain, @pass)
            mailbox_array = @imap.list('','*').collect{ |mailbox| mailbox.name }
            #mailbox_array.any? { |w| @mailbox =~ /#{w}/ }
            mailbox_array.grep(/^@mailbox/)
         end
   end

So, first I tried .any? but that doesn't return me the name of the actual mailbox. With .grep, I'm able to get a list of the mailboxes when @mailbox = "INBOX". However, when @mailbox = "Sent" it just returns [].

Here is an example of one that works (using "INBOX") and one that doesn't (using "Sent"):

#Get the list of inboxes
mailbox_array = imap.list('','*').collect{ |mailbox| mailbox.name }
=> ["INBOX", "INBOX.Trash", "INBOX.Sent", "INBOX.Sent Messages", "INBOX.Junk", "INBOX.Drafts", "INBOX.Deleted Messages", "INBOX.Apple Mail To Do"]

#Search for mailboxes including "Sent"
>> mailbox_array.grep(/^Sent/)
=> []

#Search for "INBOX"
>>             mailbox_array.grep(/^INBOX/)
=> ["INBOX", "INBOX.Trash", "INBOX.Sent", "INBOX.Sent Messages", "INBOX.Junk", "INBOX.Drafts", "INBOX.Deleted Messages", "INBOX.Apple Mail To Do"]

I think the problem is that "INBOX" is at the beginning of the strings in the array, but "Sent" is in the middle and is after a period. Not sure how to fix.

2 Answers 2

13

Try:

mailbox_array.grep(/Sent/)

The ^ means search from the start of the line.

1
  • 2
    @krapdagn: yup, although this would also match strings like "Sentiments" and "MySentMail", which you may or may not want.
    – maerics
    Feb 28, 2012 at 18:15
8

The special regex character ^ matches only the beginning of the string so perhaps you want to match a word boundary (\b) instead. Try this:

mailbox_array.grep(/\bSent\b/)
4
  • Woohoo! I will need to study these regular expressions more :). Thank you so much.
    – krapdagn
    Feb 28, 2012 at 18:08
  • OK - one more question, sorry. When mailbox = "Sent Messages" for example, it returns []. I think this is because of the double quotes. How to manipulate mailbox so that it fits in the grep(/mailbox/) without "" around it?
    – krapdagn
    Feb 28, 2012 at 18:31
  • 1
    Ah - figured it out: grep(/#{@mailbox}/)
    – krapdagn
    Feb 28, 2012 at 18:34
  • Worth noting that ^ in ruby will match the beginning of any line. \A will match the absolute beginning of the input. (Same with $ / \Z.)
    – sj26
    Jun 26, 2012 at 6:34

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.