Hicham
Hicham

Reputation: 67

Ruby - change path with gets

I have some problems with this Code. It says, i am using a wrong directory. I want to change the directory path through user-input with "gets" (e_path=gets ; subdirs(epath)). Any suggestions?

when I use subdirs("/home/etc/") everything works fine.

   def subdirs(path)
        subdirs =[]
        folderdirs=[]
        Dir.chdir(path) 
        Dir["*"].each do |x|
            if File.directory? File.join x
                subdirs << x
            else
                folderdirs << x
            end
        end
        puts "____________________________"
        puts "Projekte:\n\n"
        puts subdirs
        puts "____________________________"
        puts "Datein:\n\n"
        puts folderdirs
    end

. . .

puts "\n\nGeben Sie bitte den Pfad ein um ein Workspace zu erstellen:"
e_path=gets
sleep(1)
subdirs(e_path) #Here is the mistake

Upvotes: 1

Views: 70

Answers (1)

Max
Max

Reputation: 22325

gets includes the line terminator!

> gets
Hello, world!
=> "Hello, world!\n"

so Dir.chdir is trying to change to a directory with a newline in the name. chomp gets rid of the trailing line terminator if it exists.

Upvotes: 1

Related Questions