title: Ruby grep - searching an array for parts of a string
body: <p>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." </p>
  
  <pre><code>   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
  </code></pre>
  
  <p>So, first I tried .any? but that doesn't return me the name of the actual mailbox. With <code>.grep</code>, I'm able to get a list of the mailboxes when <code>@mailbox = "INBOX"</code>. However, when <code>@mailbox = "Sent"</code> it just returns <code>[]</code>. </p>
  
  <p>Here is an example of one that works (using "INBOX") and one that doesn't (using "Sent"):</p>
  
  <pre><code>#Get the list of inboxes
  mailbox_array = imap.list('','*').collect{ |mailbox| mailbox.name }
  =&gt; ["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"
  &gt;&gt; mailbox_array.grep(/^Sent/)
  =&gt; []
  
  #Search for "INBOX"
  &gt;&gt;             mailbox_array.grep(/^INBOX/)
  =&gt; ["INBOX", "INBOX.Trash", "INBOX.Sent", "INBOX.Sent Messages", "INBOX.Junk", "INBOX.Drafts", "INBOX.Deleted Messages", "INBOX.Apple Mail To Do"]
  </code></pre>
  
  <p>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.</p>