Skip to content
instance method

Kernel#test

(::String | ::Integer cmd, ::String | ::IO file1, ?::String | ::IO file2) -> (::TrueClass | ::FalseClass | ::Time | nil | ::Integer)
test(char, path0, path1 = nil) → object

Performs a test on one or both of the filesystem entities at the given paths path0 and path1:

  • Each path path0 or path1 points to a file, directory, device, pipe, etc.

  • Character char selects a specific test.

The tests:

  • Each of these tests operates only on the entity at path0, and returns true or false; for a non-existent entity, returns false (does not raise exception):

    Character Test
    'b' Whether the entity is a block device.
    'c' Whether the entity is a character device.
    'd' Whether the entity is a directory.
    'e' Whether the entity is an existing entity.
    'f' Whether the entity is an existing regular file.
    'g' Whether the entity’s setgid bit is set.
    'G' Whether the entity’s group ownership is equal to the caller’s.
    'k' Whether the entity’s sticky bit is set.
    'l' Whether the entity is a symbolic link.
    'o' Whether the entity is owned by the caller’s effective uid.
    'O' Like 'o', but uses the real uid (not the effective uid).
    'p' Whether the entity is a FIFO device (named pipe).
    'r' Whether the entity is readable by the caller’s effective uid/gid.
    'R' Like 'r', but uses the real uid/gid (not the effective uid/gid).
    'S' Whether the entity is a socket.
    'u' Whether the entity’s setuid bit is set.
    'w' Whether the entity is writable by the caller’s effective uid/gid.
    'W' Like 'w', but uses the real uid/gid (not the effective uid/gid).
    'x' Whether the entity is executable by the caller’s effective uid/gid.
    'X' Like 'x', but uses the real uid/gid (not the effective uid/git).
    'z' Whether the entity exists and is of length zero.
  • This test operates only on the entity at path0, and returns an integer size or nil:

    Character Test
    's' Returns positive integer size if the entity exists and has non-zero length, nil otherwise.
  • Each of these tests operates only on the entity at path0, and returns a Time object; raises an exception if the entity does not exist:

    Character Test
    'A' Last access time for the entity.
    'C' Last change time for the entity.
    'M' Last modification time for the entity.
  • Each of these tests operates on the modification time (mtime) of each of the entities at path0 and path1, and returns a true or false; returns false if either entity does not exist:

    Character Test
    '<' Whether the mtime at path0 is less than that at path1.
    '=' Whether the mtime at path0 is equal to that at path1.
    '>' Whether the mtime at path0 is greater than that at path1.
  • This test operates on the content of each of the entities at path0 and path1, and returns a true or false; returns false if either entity does not exist:

    Character Test
    '-' Whether the entities exist and are identical.
See also #tap #then #system #throw also in 4.1 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0