Use the File.exist? method to check for the existence of a file:
puts "It exists!" if File.exist?("main.rb")
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