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
file.c
View on GitHub ↗
static VALUE
rb_file_writable_p(VALUE obj, VALUE fname)
{
return RBOOL(rb_eaccess(fname, W_OK) >= 0);
}
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.
See also
#world_writable?
#writable_real?
#world_readable?
#zero?
also in
4.0 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0