-1

If I don't pass in any options in the options hash, how does FileUtils behave when it fails?

I tried looking at FileUtils on ruby-doc.org and APIdock and couldn't find anything.

mu is too short
  • 426,620
  • 70
  • 833
  • 800
Andrew Grimm
  • 78,473
  • 57
  • 200
  • 338

1 Answers1

0

how does FileUtils behave when it fails?

Well, let's see:

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)

So...you could do this:

require 'FileUtils'

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

--output:--
Couldn't switch directories

Or, even this:

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
7stud
  • 46,922
  • 14
  • 101
  • 127