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.
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?
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.

Written byAshik Salman
Related blogs
- 01Rails 7.1 allows subscribing to transaction eventsJanuary 16, 2024 · Vishnu M
- 02Rails 7.1 adds path_params option to url_forNovember 7, 2023 · Neenu Chacko
- 03Rails 7.1 optimizes the default SQLite3 configurationOctober 31, 2023 · Vishnu M
- 04Rails 7.1 adds multi-column ordering to batch processingOctober 3, 2023 · Navaneeth D