Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7799625
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T00:22:21+00:00 2026-06-02T00:22:21+00:00

I am working on a Ruby script that will download emails from Gmail and

  • 0

I am working on a Ruby script that will download emails from Gmail and download attachments matching a specific pattern. I am basing this off the excellent Mail gem for Ruby. I am using Ruby 1.9.2. I am not that experienced with Ruby and appreciate any help offered.

In the code below, emails is an array of emails returned from gmail that contain a specific label. What I am stuck on is looping through the array of emails and processing what may be multiple attachments on each email. The inner loop of emails[index].attachments.each does work if I specify an index value, I have not been successful in wrapping the first loop to go through 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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T00:22:23+00:00Added an answer on June 2, 2026 at 12:22 am

    the syntax for each_with_index is like this:

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

    You should then replace the line
    emails.each_with_index do |index|

    with

    emails.each_with_index do |email,index|
    

    However I don’t see you actually using the index so you could probalby simplify it to this:

    emails.each do |email|
        email.attachments.each do | attachment |
    ....
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on a Ruby script that will dump a key, value pair
I'm working on a ruby script that ultimately starts up a system process that
I have been working on a Ruby Script that enters in data into a
I am working on a script that collects all comments from my DB and
Working on a little Ruby script that goes out to the web and crawls
I'm working on a ruby script to grab historical stock prices from Yahoo, using
I'm working off these instructions: http://github.com/dcparker/ruby-gmail From the home directory I do a standard
We're working on a Ruby on Rails app that needs to take advantage of
I came from Java, and now I am working more with Ruby. One language
I need to convert a init.d script that uses ruby to not use ruby.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.