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 = + "/Desktop/millionaires")
>> File.exists?(millionaires_path)
=> true

Leave a Reply

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

You are commenting using your 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