2016-06-17 6 views

答えて

0

FileUtilsはどのように失敗するのですか?

まあ、見てみましょう:

require 'FileUtils' 

puts Dir.pwd 
#Existing dir: 
FileUtils.cd("../python_programs") 
puts Dir.pwd 
FileUtils.cd("/does/not/exist") 

--output:-- 
/Users/7stud/ruby_programs 
/Users/7stud/python_programs 
....`chdir': No such file or directory @ dir_chdir - /does/not/exist (Errno::ENOENT) 

そう...あなたはこれを行うことができます:

require 'FileUtils' 

begin 
    FileUtils.cd("/does/not/exist") 
rescue Errno::ENOENT 
    puts "Couldn't switch directories" 
end 

--output:-- 
Couldn't switch directories 

それとも、これさえ:

require 'FileUtils' 

paths = %w[ 
    ../python_programs 
    /does/not/exist 
    ../rails_projects 
] 

paths.each do |path| 
    begin 
    FileUtils.cd(path) 
    puts "Just switched directories to: #{Dir.pwd}" 
    rescue Errno::ENOENT 
    puts "Couldn't switch to directory: #{path}" 
    end 
end 

--output:-- 
Just switched directories to: /Users/7stud/python_programs 
Couldn't switch to directory: /does/not/exist 
Just switched directories to: /Users/7stud/rails_projects 
関連する問題