---
title: "Rails 7 adds Pathname#existence"
description: "Rails 7 adds Pathname#existence"
canonical_url: "https://www.bigbinary.com/blog/rails-7-adds-pathname-existence"
markdown_url: "https://www.bigbinary.com/blog/rails-7-adds-pathname-existence.md"
---

# Rails 7 adds Pathname#existence

Rails 7 adds Pathname#existence

- Author: Ashik Salman
- Published: December 7, 2021
- Categories: Rails, Rails 7

Rails 7 introduces
[`Pathname#existence`](https://api.rubyonrails.org/classes/Pathname.html#method-i-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.

```ruby
=> 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.

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

Please check out this [pull request](https://github.com/rails/rails/pull/43726)
for more details.

## Links

- [Human page](https://www.bigbinary.com/blog/rails-7-adds-pathname-existence)
