Quoting by array index

I am working on a Ruby script that will download emails from Gmail and download attachments matching a specific template. I base this on the excellent Mail gem for Ruby. I am using Ruby 1.9.2. I am not so good at Ruby and appreciate any help.

In the code below, emails are an array of emails returned from gmail containing a specific label. What I'm stuck with is a loop through an array of letters and processing what may be multiple attachments for each letter. The inner loop of email messages [index] .attachments.each works, if I specify an index value, I could not complete the first loop to see all the index values โ€‹โ€‹of the array.

emails = Mail.find(:order => :asc, :mailbox => 'label')

emails.each_with_index do |index|
    emails[index].attachments.each do | attachment |
      # Attachments is an AttachmentsList object containing a
      # number of Part objects
      if (attachment.filename.start_with?('attachment'))
        filename = attachment.filename
        begin
            File.open(file_dir + filename, "w+b", 0644) {|f| f.write attachment.body.decoded}
        rescue Exception => e
            puts "Unable to save data for #{filename} because #{e.message}"
        end
      end
    end
end
+3
2

each_with_index :

@something.each_with_index do |thing,index|
    puts index, thing
end

  emails.each_with_index do | index |

emails.each_with_index do |email,index|

, , :

emails.each do |email|
    email.attachments.each do | attachment |
....
+10

, each_with_index, , .

emails.each_with_index do |o, i|
  o.attachments.each do | attachment |

, , each.

+3

All Articles