Ruby - File File Exists

Introduction

Use the File.exist? method to check for the existence of a file:

Demo

puts "It exists!" if File.exist?("main.rb")

Result

File.exist? returns true if the named file exists.

The following code check if file exists.


class MyFile 
  attr_reader :handle 

  def initialize(filename) 
    if File.exist?(filename) 
      @handle = File.new(filename, "r")  
    else 
      return false 
    end 
  end 
end 

Related Topic