Ruby file operation knowledge accumulation

Source: Internet
Author: User
This section describes the fileutils module of the file class filetest module of the Dir class.

-------------------------------------------------------------
Directory operation: Use the static method of the Dir class or create an Instance Object of the Dir class.

-------------------------------------------------------------

A static method of Dir class
The dir class provides a set of static methods for creating, deleting, and traversing directories. You can specify the full path name to add, delete, and query directories. The default operation object is the current script working directory.

Get the current script working directory
Dir: Pwd property or dir. getwd ()

Change the current script working directory
Dir: chdir

Create directory
Dir: mkdir
If the full directory name is not specified, the default value is the working directory.

Delete directory
Dir: rmdir
If the full directory name is not specified, the default value is the working directory.

Traverse directories
Dir: foreach (arg1) {| Item1 | segment ..}
If the full directory name is specified in the arg1 parameter, for example, "d:/Ruby/rubywork/", the specified directory is traversed.
If the args1 parameter is not a full directory name, for example, "rubywork", traverse "current working directory/rubywrok /".

How to get the current Script directory
We know that the global variable $0 is the full path of the current script. Therefore, you can use file. dirname ($0) to obtain the directory of the current script.

(Note: The working directory, also known as the environment directory, is the working directory when the current software is running. The operations currently executed by the software are based on this working directory, and the working directory is generally the directory where the script is located, but the working directory is not equal to the directory where the script is located)

Ii. dir class instance object
You can use dir. New to create an Instance Object of the Dir class. A parameter is required, which specifies a directory. The methods for calling this object will be based on this directory.

Dir # Close
Close this object and release the occupied Resources

Dir # Each
Traverses an object in the same way as dir: foreach, but there is no parameter to traverse the subfile of the current object.

Dir # path
Returns the directory of the Instance Object.

Dir # Read
Returns a subfile.

Dir # POS
Returns the pointer to the current sub-file.

Dir # Pos =
Set the sub-file pointer

Dir # rewind
Set the sub-file pointer to the starting position

Dir # Seek
Set the sub-file pointer

Dir # Tell
Get current pointer

-------------------------------------------------
File Operations: use static methods of the file class or instance objects of the file class
-------------------------------------------------
Three static file Methods
File: atime (filename)
Returns the last access time of the specified object.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.