Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →
instance method
File::Stat#readable?
() -> bool
readable? → true or false
file.c
View on GitHub ↗
static VALUE
rb_stat_r(VALUE obj)
{
rb_io_stat_data *st = get_stat(obj);
#ifdef USE_GETEUID
if (geteuid() == 0) return Qtrue;
#endif
#ifdef S_IRUSR
if (rb_stat_owned(obj))
return RBOOL(st->ST_(mode) & S_IRUSR);
#endif
#ifdef S_IRGRP
if (rb_stat_grpowned(obj))
return RBOOL(st->ST_(mode) & S_IRGRP);
#endif
#ifdef S_IROTH
if (!(st->ST_(mode) & S_IROTH)) return Qfalse;
#endif
return Qtrue;
}
Returns whether the entry represented by self exists and is readable by the owner and group of the current process; see Permissions:
Example
path = '/tmp/secret.txt' File.write(path, 'foo') File.stat(path).readable? # => true File.chmod(0o000, path) File.stat(path).readable? # => false File.delete(path) # Clean up.