Guest User

Untitled

a guest
Aug 1st, 2018
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.13 KB | None | 0 0
  1. Ruby grep - searching an array for parts of a string
  2. class ExamineMail
  3. def initialize(user, domain, pass, box)
  4. @username = user
  5. @domain = domain
  6. @pass = pass
  7. @mailbox = box
  8. end
  9.  
  10. def login()
  11. @imap = Net::IMAP.new("mail." + @domain)
  12. @imap.authenticate('LOGIN', @username + "@" + @domain, @pass)
  13. mailbox_array = @imap.list('','*').collect{ |mailbox| mailbox.name }
  14. #mailbox_array.any? { |w| @mailbox =~ /#{w}/ }
  15. mailbox_array.grep(/^@mailbox/)
  16. end
  17. end
  18.  
  19. #Get the list of inboxes
  20. mailbox_array = imap.list('','*').collect{ |mailbox| mailbox.name }
  21. => ["INBOX", "INBOX.Trash", "INBOX.Sent", "INBOX.Sent Messages", "INBOX.Junk", "INBOX.Drafts", "INBOX.Deleted Messages", "INBOX.Apple Mail To Do"]
  22.  
  23. #Search for mailboxes including "Sent"
  24. >> mailbox_array.grep(/^Sent/)
  25. => []
  26.  
  27. #Search for "INBOX"
  28. >> mailbox_array.grep(/^INBOX/)
  29. => ["INBOX", "INBOX.Trash", "INBOX.Sent", "INBOX.Sent Messages", "INBOX.Junk", "INBOX.Drafts", "INBOX.Deleted Messages", "INBOX.Apple Mail To Do"]
  30.  
  31. mailbox_array.grep(/Sent/)
Add Comment
Please, Sign In to add comment