Ruby File classes and methods


Release date:2023-10-31 Update date:2023-11-01 Editor:admin View counts:200

Label:

Ruby File classes and methods

File represents a connection to a normal file stdio object. open returns an instance of this class for a normal file.

Class method

Serial number

Method and description

1

File::atime( path) returns the last access time of the path.

2

File::basename( path[, suffix]) Returns the file name at the end of the path. If suffix is specified, it is removed from the end of the file name. For example: File.basename (”/ home/users/bin/ruby.exe”) # = > “ruby.exe”

3

File::blockdev?( path returns true if path is a block device.

4

File::chardev?( path) returns true if path is a character device.

5

File::chmod( mode, path...) changes the permission mode of the specifiedfile.

6

File::chown( owner, group, path...) changes the owner and group of the specified file.

7

File::ctime( path) returns the last inode change time of the path.

8

File::delete( path...) File::unlink( path...) deletes the specified file.

9

File::directory?( path) if path is a directory, true is returned.

10

File::dirname( path) returns the directory portion of the path, excluding the last file name.

11

File::executable?( path) returns true if path is executable.

12

File::executable_real?( path) if path is executable with true user rights, it returns true.

13

File::exist?( path) returns true if path exists.

14

File::expand_path( path[, dir]) return the absolute path of path, extend~ to the home directory of the process owner, and ~ user to the home directory of the user. The relative path is relative to the directory specified by dir, or to the current working directory if dir is omitted.

15

File::file?( path) if path is a normal file, true is returned.

16

File::ftype( path) returns one of the following strings, indicating the file type: file-normal file directory-directory characterSpecial-character special file blockSpecial-block special file fifo-named pipe (FIFO) link-symbolic link socket-Socket unknown-unknown file type

17

File::grpowned?( path) returns true if path is owned by the group to which the user belongs.

18

File::join( item...) returns a string concatenated by the specified items and uses the File::Separator to separate. For example: File::join (“”, “home”, “usrs”, “bin”) # = > “/ home/usrs/bin”

19

File::link( old, new) create a hard link to the file old.

20

File::lstat( path) is the same as stat, but it returns information on its own symbolic link, not the file it points to.

21

File::mtime( path) returns the time when the path was last modified.

22

File::new( path[, mode="r"]) File::open( path[, mode="r"]) File::open( path[, mode="r"]) {|f| ...} Open the file. If a block is specified, the block is executed by passing a new file as a parameter. When the block exits, the file closes automatically. These methods are different from Kernel.open in that even if path starts with |, subsequent strings are not run as commands.

23

File::owned?( path) returns true if path is owned by a valid user.

24

File::pipe?( path) returns true if path is a pipe.

25

File::readable?( path) returns true if path is readable.

26

File::readable_real?( path) if path is readable through true user rights, it returns true.

27

File::readlink( path) returns the file that path points to.

28

File::rename( old, new) change the file name old to new.

29

File::setgid?( path) if the set-group-id permission bit of path is set, true is returned.

30

File::setuid?( path) if the set-user-id permission bit of path is set, true is returned.

31

File::size( path) Returns the file size of the path.

32

File::size?( path) Returns the file size of path, or nil if 0.

33

File::socket?( path) Returns true if path is a socket.

34

File::split( path) Returns an array containing the contents of path, andthe path is divided into File::dirname(path) and File::basename(path) .

35

File::stat( path) object with information on the path File::Stat object.

36

File::sticky?( path) Returns true if the sticky bit of path is set.

37

File::symlink( old, new) Create a symbolic link to the file old.

38

File::symlink?( path) Returns true if path is a symbolic link.

39

File::truncate( path, len) Truncate the specified file as len bytes.

40

File::unlink( path...) Deletes the file given by path.

41

File::umask([ mask]) If no parameter is specified, the current umask is returned for the process. If a parameter is specified, umask is set and the old umask is returned.

42

File::utime( atime, mtime, path...) Changes the access and modification time of the specified file.

43

File::writable?( path) Returns true if path is writable.

44

File::writable_real?( path) If path is writable with true user rights, true is returned.

45

File::zero?( path) Returns true if the file size of path is 0.

Example method

Assuming f is an instance of the ``File` class:

Serial number

Method and description

1

F.atime returns the last access time of f.

2

f.chmode( mode) Change the permission mode of f.

3

f.chown( owner, group) Change the owner and group of f.

4

F.ctime returns the last inode change time of f.

5

F.flock (op) calls flock (2). Op can be 0 or a logical value or File class constants LOCK_EX, LOCK_NB, LOCK_SH, and LOCK_UN.

6

F.lstat is the same as stat, but it returns information on its own symbolic link, not the file it points to.

7

F.mtime returns the last modification time of f.

8

F.path returns the pathname used to create f.

9

f.reopen( path[, mode="r"]) Reopen the file.

10

f.truncate( len) Truncate f to len bytes.

Powered by TorCMS (https://github.com/bukun/TorCMS).