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#writable?

(::path file_name) -> bool
File.writable?(object) → true or false

Returns whether given object exists and is writable by the owner and group in the current process:

Example
filepath = '/tmp/secret.txt'
File.writable?(filepath)   # => false  # Non-existent.
File.write(filepath, 'foo')            # Create file.
File.writable?(filepath)   # => true   # Writable.
File.chmod(0o000, filepath)            # Make non-writable.
File.writable?(filepath)   # => false  # Not writable.
File.delete(filepath)                  # Clean up.
File.writable?('/etc')     # => false  # Directory.
File.writable?($stdin)     # => false  # IO object.

Note that filesystem security features may cause this method to return true even when the file is not writable by the owner and group.