Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby grep - searching an array for parts of a string

Tags:

grep

ruby

imap

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.

like image 260
krapdagn Avatar asked Feb 28 '12 18:02

krapdagn


People also ask

What are strings&arrays in Ruby?

Both strings & arrays are very important building blocks for writing your Ruby programs. If you don’t understand how these 2 work you won’t be able to write even the most basic projects you can think of. Don’t just read this…

How do you do triple equals in Ruby with grep?

The trick is with the === method (triple equals) in Ruby. Grep calls this method on whatever argument you pass to it. And it turns out that classes, regular expressions & ranges all implement ===.

Why is it important to learn strings and arrays in Ruby?

Both strings & arrays are very important building blocks for writing your Ruby programs. If you don’t understand how these 2 work you won’t be able to write even the most basic projects you can think of.

How do I use grep to filter enumerable objects?

You can use Grep to filter enumerable objects, like Arrays & Ranges. “But select already does that!” Yes, but grep works in a different way & it produces different results. Let’s see some examples. If you have an array of words: You can find all the words that start with “a”: And if you have a list of numbers:


1 Answers

Try:

mailbox_array.grep(/Sent/)

The ^ means search from the start of the line.

like image 54
Asher Avatar answered Sep 19 '22 10:09

Asher