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 652097
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T22:14:05+00:00 2026-05-13T22:14:05+00:00

I have a Ruby 1.8.6 script that is supposed to take a filename that

  • 0

I have a Ruby 1.8.6 script that is supposed to take a filename that contains a path, create the folders then move the file into the folders. I have an array created_paths to keep tracked of folders created (the script will loop through lots of files). I am having a problem adding to the created_paths array.

created_paths = Array.new
file_name = "first\\second\\third.txt"
parts = file_name.split('\\')
tmp_path = ""
parts.each_with_index { |part,i|
    if i == (parts.length - 1)                
        # copy file to new dir structure
    else
        tmp_path << part << "/"
        if !created_paths.include?(tmp_path)                   
            puts "add to array: #{tmp_path}"
            created_paths.push(tmp_path)
            # create folder
        end
    end
}
puts "size=#{created_paths.length}\n"
created_paths.each { |z| print z, "\n " }

When I push tmp_path on to the created_paths array it seems the reference to tmp_path has been added and not the value. On the second iteration of the loop created_paths.include?(tmp_path) is returning True. How do I get the value of tmp_path to be stored in my array, or maybe there is a scope issue i’m missing ?

My output:

add to array: first/
size=1
first/second/

My excepted output:

add to array: first/
add to array: first/second/
size=2
first/
first/second/
  • 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-13T22:14:05+00:00Added an answer on May 13, 2026 at 10:14 pm

    You can use tmp_path.dup to clone the string before pushing it to the array.

    But I don’t understand why you’re doing all of it (keeping the list of created directories). Take a look at FileUtils#mkdir_p, you pass it a path to a directory you want to create and it creates it, along with all the missing parent directories.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.