How to check if a given directory exists in Ruby

后端 未结 5 1573
难免孤独
难免孤独 2020-12-22 20:17

I am trying to write a script which automatically checks out or updates a Subversion URL based on whether a specified directory exists or not.

For some reason, my co

5条回答
  •  我在风中等你
    2020-12-22 21:12

    All the other answers are correct, however, you might have problems if you're trying to check directory in a user's home directory. Make sure you expand the relative path before checking:

    File.exists? '~/exists'
    => false
    File.directory? '~/exists'
    => false
    File.exists? File.expand_path('~/exists')
    => true
    

提交回复
热议问题