Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →
instance method
File#chmod
(::int mode) -> (0 | nil)
chmod(mode) → 0
file.c
View on GitHub ↗
static VALUE
rb_file_chmod(VALUE obj, VALUE vmode)
{
rb_io_t *fptr;
mode_t mode;
#if !defined HAVE_FCHMOD || !HAVE_FCHMOD
VALUE path;
#endif
mode = NUM2MODET(vmode);
GetOpenFile(obj, fptr);
#ifdef HAVE_FCHMOD
if (rb_fchmod(fptr, mode) == -1) {
if (HAVE_FCHMOD || errno != ENOSYS)
rb_sys_fail_path(fptr->pathv);
}
else {
if (!HAVE_FCHMOD) return INT2FIX(0);
}
#endif
#if !defined HAVE_FCHMOD || !HAVE_FCHMOD
if (NIL_P(fptr->pathv)) return Qnil;
path = rb_str_encode_ospath(fptr->pathv);
if (rb_chmod(RSTRING_PTR(path), mode) == -1)
rb_sys_fail_path(fptr->pathv);
#endif
return INT2FIX(0);
}
Changes the mode of self; returns ‘0’. See Filesystem Modes and especially Setting a Mode.
Example
filepath = '/tmp/t.tmp' File.write(filepath, 'foo') '%06o' % File.stat(filepath).mode # => "100664" file = File.new(filepath) file.chmod(0o755) '%06o' % File.stat(filepath).mode # => "100755" # Clean up. file.close File.delete(filepath)