File#exists?

When a new Rails application is initialized, the File#exists? method is used in the config/ directory. The File#exists? predicate method returns true if a file exists and false otherwise.

See if a file called “millionaires” exists in the Desktop directory.

>> File.exists?(Dir.home + "/Desktop/millionaires")
=> false

Create a file in the Desktop directory called “millionaires” and verify that the file has been created with File#exists?

>> require 'fileutils'
>> FileUtils.touch(Dir.home + "/Desktop/millionaires")
>> File.exists?(Dir.home + "/Desktop/millionaires")
=> true

The File#exists? method accepts a String or Pathname object as the argument.

>> require 'pathname'
>> millionaires_path = Pathname.new(Dir.home + "/Desktop/millionaires")
>> File.exists?(millionaires_path)
=> true
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s