Skip to content

Welcome to Tech by Example

Menu
  • Home
  • Posts
  • System Design Questions
Menu

Check if a file exists in Ruby Language

Posted on December 26, 2022December 26, 2022 by admin

Overview

File class in ruby provides a method that returns true if a given file exists and returns false if a given false doesn’t exist

https://ruby-doc.org/core-3.0.0/File.html#method-c-exist-3F

Here is the signature of this method

exist?(file_name) → true or false

Example

Below is the program for the same.

File.open("temp.txt", "w") do |f|     
    f.write("Test")   
  end
  
puts File.exist?('temp.txt')

Output

true

Note: Check out our system design tutorial series System Design Questions

©2025 Welcome to Tech by Example | Design: Newspaperly WordPress Theme