Skip to content

Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →

instance method

Pathname#chmod

(::Integer mode_int) -> ::Integer
chmod(mode) → 1

Changes the mode of the entry at the path in self; returns 1. See Filesystem Modes and especially Setting a Mode.

These examples use a helper method, mode, that displays a mode both in octal digits and in characters:

Example
dirpath = 'doc/foo'
dir_pn = Pathname(dirpath)
dir_pn.mkdir         # Create directory.
mode(dirpath)        # => "040775 drwxrwxr-x"
filepath = File.join(dirpath, 't.tmp')
file_pn = Pathname(filepath)
file_pn.write('bar') # Create file.
mode(filepath)       # => "100664 -rw-rw-r--"
file_pn.chmod(0755)  # Change file mode.
mode(filepath)       # => "100755 -rwxr-xr-x"
dir_pn.chmod(0644)   # Change directory mode.
mode(dirpath)        # => "040644 drw-r--r--"
dir_pn.rmtree        # Clean up.
See also #children #chop_basename #chardev? #chown also in 4.0 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0