December 7, 2021
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 exist 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.
If this blog was helpful, check out our full blog archive.