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

  • Home
  • SEARCH
  • 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 6727911
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:06:22+00:00 2026-05-26T10:06:22+00:00

very new to Ruby, I’ve got the following situation. I have a file with

  • 0

very new to Ruby, I’ve got the following situation. I have a file with values separated by new lines, they look like this:

18917
18927
18929
...

I want to prepend a folder path to all of them, then grab the first 2 characters and prepend that as well, then the value in the file and then append a ‘.jpg’ at the end so they would end up looking like this:

path/to/foler/18/18917.jpg

So I’ve code this ruby code:

folder = "/path/to/folder" 
lines = File.readlines("values.csv")
images = lines.collect.to_s.gsub("\n", ".jpg,") 
images.split(',').collect { |dogtag |  puts "bad dog: #{folder}/#{dogtag[0,2]}/#{dogtag}" }

Now, this almost works, the part that is not working is the grabbing of the first 2 characters. I also tried it with the method outside quotes (and without the #{} of course) but it just produces an empty result.

Could someone please point out my folly?

Eventually I want to delete those images but I’m guessing that substituting ‘File.delete’ for ‘puts’ above would do the trick?

As usual, thanks in advance for taking the time to look at this.

  • 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-05-26T10:06:23+00:00Added an answer on May 26, 2026 at 10:06 am

    You don’t seem to be understanding what collect does.

    I would rewrite your snippet like this:

    File.read("values.csv").each do |line|
      puts "bad dog: /path/to/folder/#{line[0,2]}/#{line.chomp}.jpg"
    end
    

    — Update for last comment: —

    If you don’t want to use an if statement to check if a file exists before deleting it, you have two option (AFAIK).

    Use rescue:

    File.read("values.csv").each do |line|
      begin
        File.delete "/path/to/folder/#{line[0,2]}/#{line.chomp}.jpg"
      rescue Errno::ENOENT
        next
      end
    end
    

    Or extend the File class with a delete_if_exists method. You can just put this at the top of your script:

    class File
      def self.delete_if_exists(f)
        if exists?(f)
          delete(f)
        end
      end
    end
    

    With this, you can do:

    File.read("values.csv").each do |line|
      File.delete_if_exists "/path/to/folder/#{line[0,2]}/#{line.chomp}.jpg"
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm very new to Ruby on Rails, and have been getting the following error
I have built a very simple blog application using Ruby on Rails. New to
I'm fairly new in the Ruby + Rails scene. Although I have a very
I'm very new to ruby & Sinatra and I have a basic question: The
I'm very new to CMake but have experience with general build environments like Make.
I am very new to ruby / rails and have an issue that I
I am still very new to Ruby (reading through the Pickaxe and spending most
I am very new to Ruby so could you please suggest the best practice
I'm very new to Ruby on Rails so please go easy! I've uploaded the
I'm very new to ruby. I'm trying to search for any instance of a

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.