Skip to content

Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →

instance method

FileTest#identical?

(::path | ::io file_1, ::path | ::io file_2) -> bool
File.identical?(object_0, object_1) → true or false

Returns whether the given objects represent filesystem entries that are identical; each object may be a string path or an IO object:

Example
# Paths.
File.identical?('README.md', 'README.md')   # => true  # Same path.
File.identical?('README.md', './README.md') # => true  # Same entry.
File.identical?('.', '.')                   # => true  # Directory.
File.identical?('README.md', 'LEGAL')       # => false
File.identical?('README.md', 'nosuch')      # => false # Non-existent entry.
# Links and File object.
File.link('README.md', 'link')              # Symbolic link.
File.symlink('README.md', 'symlink')        # Hard link.
file = File.open('README.md', 'r')          # File object.
File.identical?('README.md', 'link')        # => true
File.identical?('README.md', 'symlink')     # => true
File.identical?('README.md', file)          # => true
# Clean up.
File.unlink('link')
File.unlink('symlink')
file.close
See also #grpowned? #owned? #file? #pipe? also in 4.0 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0