Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →
instance method
File::Stat#writable?
() -> bool
writable? → true or false
file.c
View on GitHub ↗
static VALUE
rb_stat_w(VALUE obj)
{
rb_io_stat_data *st = get_stat(obj);
#ifdef USE_GETEUID
if (geteuid() == 0) return Qtrue;
#endif
#ifdef S_IWUSR
if (rb_stat_owned(obj))
return RBOOL(st->ST_(mode) & S_IWUSR);
#endif
#ifdef S_IWGRP
if (rb_stat_grpowned(obj))
return RBOOL(st->ST_(mode) & S_IWGRP);
#endif
#ifdef S_IWOTH
if (!(st->ST_(mode) & S_IWOTH)) return Qfalse;
#endif
return Qtrue;
}
Returns whether the entry at the path in self exists and is writable by the effective owner and group in the current process:
Example
filepath = '/tmp/secret.txt' File.write(filepath, 'foo') File.stat(filepath).writable? # => true # Writable. File.chmod(0o000, filepath) # Make non-writable. File.stat(filepath).writable? # => false # Not writable. File.delete(filepath) # Clean up. File.stat('/etc').writable? # => false # Directory.
Note that filesystem security features may cause this method to return true even when the file is not writable by the effective 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