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
pathname_builtin.rb
View on GitHub ↗
# File tmp/rubies/ruby-4.1.0-preview1/pathname_builtin.rb, line 1606
def chown(owner, group) File.chown(owner, group, @path) end
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
nilor +-1+ owner or group id is ignored. -
The method follows symbolic links to the target entry.