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#chown

(::Integer owner, ::Integer group) -> ::Integer
chown(owner_id, group_id) → 0

Changes the owner and group of an entry (directory or file):

Example
# Super user; all privileges.
Process.uid                    # => 0
Process.gid                    # => 0

# Pathname for a (non-existent) directory.
dir_pn = Pathname('doc/foo')   # => #<Pathname:doc/foo>
# Create the directory; fetch original owner and group.
dir_pn.mkdir
dir_stat = dir_pn.stat
dir_stat.uid                   # => 0
dir_stat.gid                   # => 0
# Change owner; fetch current owner and group.
dir_pn.chown(1000, 1000)
dir_stat = dir_pn.stat
dir_stat.uid                   # => 1000
dir_stat.gid                   # => 1000

Pathname for a (non-existent) file in the directory.
file_pn = dir_pn.join('t.tmp') # => #<Pathname:doc/foo/t.tmp>
# Create the directory; fetch original owner and group.
file_pn.write('foo')
file_stat = file_pn.stat
file_stat.uid                  # => 0
file_stat.gid                  # => 0
# Change owner; fetch current owner and group.
file_pn.chown(1000, 1000)
file_stat = file_pn.stat
file_stat.uid                  # => 1000
file_stat.gid                  # => 1000
# Clean up.
file_pn.delete
dir_pn.rmdir

Notes:

  • On Windows, the owner and group are not changed.

  • Only a process with superuser privileges can change the owner of an entry.

  • The owner of an entry can change its group to any group to which the owner belongs.

  • A nil or +-1+ owner or group id is ignored.

  • The method follows symbolic links to the target entry.

See also #chop_basename #cleanpath #chmod #ctime also in 4.0 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0