← Blog

Rails 7 adds Pathname#existence

This blog is part of our Rails 7 series.

Rails 7 introduces Pathname#existence method, which returns the receiver if the given file path exists, otherwise returns nil.

Before

We need to first check whether the given file path exists or not to perform any other operations on it.

=> file_path = "config/schedule.yml"
=> Pathname.new(file_path).read if Pathname.new(file_path).exist?

Rails 7 onwards

The Pathname#existence method acts like Object#presence for file existence.

=> file_path = "config/schedule.yml"
=> Pathname.new(file_path).existence&.read

Please check out this pull request for more details.

Ashik Salman avatar
Written byAshik Salman
Newsletter

Stay up to date with our blogs.

Subscribe to receive an email notification for a new blog post.