红宝石目录
目录是可以存储文件的位置。对于 Ruby,Dir 类和 FileUtils 模块管理目录,File 类处理文件。双点 (..)是指目录的父目录,单点 (.)是指目录本身。
目录类
Dir 类提供对 Ruby 中文件系统目录结构的访问和内容。它提供了一种列出文件夹内容、生成带有适当路径分隔符的文件名等的方法。
这些是 Dir 类的一些特性:
- 创建 Ruby 目录:
Dir 类中的mkdir()方法用于创建目录。我们可以使用下面的代码创建非嵌套目录,如果目录创建成功, mkdir()方法返回 0。
句法:
Dir.mkdir "name_of_directory"
- 例子:
Ruby
# creating directory
f=Dir.mkdir "abc"
# a directory named abc is created
print("#{f}")
Ruby
# creating directory
puts Dir.mkdir("folder")
# checking if the directory exists or not
puts Dir.exists?("folder")
Ruby
# creating directory
puts Dir.mkdir("folder")
# checking if the directory is empty or not
puts Dir.empty?("folder")
Ruby
# creating directory
Dir.mkdir("folder")
# returns current working directory
puts Dir.pwd
Ruby
# creating directory
Dir.mkdir("folder")
# returns home directory
puts Dir.home
Ruby
# creating directory
Dir.mkdir("folder")
# creating object of that directory using new() method
obj=Dir.new("folder")
# assigns the path parameter of obj to variable f
f=obj.path
print("#{f}")
Ruby
# creating directory
Dir.mkdir("folder")
# returns the path of the current working directory
puts Dir.getwd
Ruby
# creating directories
Dir.mkdir("/workspace/folder1")
Dir.mkdir("/workspace/folder2")
# displaying the path of the current directory
puts Dir.pwd
# changing the current working directory
Dir.chdir("folder2")
puts Dir.pwd
Ruby
# creating a directory named folder
Dir.mkdir("folder")
# displaying the path of the current directory
puts Dir.pwd
# changing current working directory to folder
Dir.chdir("folder")
puts Dir.pwd
# creating directories inside folder
Dir.mkdir("subfolder1")
Dir.mkdir("subfolder2")
Dir.mkdir("subfolder3")
# displays all the files and folders present in folder
print("Entries:\n")
puts Dir.entries("C:/Users/KIIT/Desktop/folder")
Ruby
# creating a directory named folder
Dir.mkdir("folder")
# changing current working directory to folder
Dir.chdir("folder")
# creating directories inside folder
Dir.mkdir("dabce")
Dir.mkdir("abcd")
Dir.mkdir("program.rb")
Dir.mkdir("program2.rb")
# displaying specified files and folders
print"\nAll files in the current working directory: \n"
puts Dir.glob("*")
print"\nAll files containing 'abc' in the name: \n"
puts Dir.glob("*abc*")
print"\nAll ruby files: \n"
puts Dir.glob("*.rb")
Ruby
#creating directory
Dir.mkdir("folder")
puts Dir.exist?("folder")
# deleting directory
Dir.rmdir("folder")
puts Dir.exist?("folder")
Ruby
# creating directory parent_folder
Dir.mkdir "parent_folder"
print("Current Directory: ")
puts Dir.pwd
require "fileutils"
# creating nested directory in parent_folder
FileUtils.mkdir_p "parent_folder/child_folder/folder"
# changing current directory to parent_folder
Dir.chdir("/workspace/parent_folder")
print("Current Directory: ")
puts Dir.pwd
# checking child folder exists or not
puts Dir.exists?("child_folder")
# changing current directory to child_folder
Dir.chdir("/workspace/parent_folder/child_folder")
print("Current Directory: ")
puts Dir.pwd
# checking folder exists or not
puts Dir.exists?("folder")
Ruby
# creating directories
Dir.mkdir "folder1"
Dir.mkdir "folder2"
require "fileutils"
# moving directory folder1 into directory folder2
FileUtils.mv( "folder1", "folder2")
# changing current directory to folder2
Dir.chdir("folder2")
# checking if folder1 exists in folder 2
puts Dir.exists?("folder1")
Ruby
require "fileutils"
# copying test.txt from folder1 to folder2
FileUtils.cp( "folder2/test.txt", "folder1")
Dir.chdir("folder1")
# checking if test.txt exists in folder1
puts File.exist?("test.txt")
- 输出:
0
- 检查 Ruby 目录:
- Dir 类中的exists()方法用于检查目录是否存在。
句法:
- Dir 类中的exists()方法用于检查目录是否存在。
Dir.exist?"name_of_directory"
- 例子:
红宝石
# creating directory
puts Dir.mkdir("folder")
# checking if the directory exists or not
puts Dir.exists?("folder")
- 输出:
0
true
- 空的? Dir 类中的方法用于检查目录是否为空。
句法:
Dir.empty?"name_of_directory"
- 例子:
红宝石
# creating directory
puts Dir.mkdir("folder")
# checking if the directory is empty or not
puts Dir.empty?("folder")
- 输出:
0
true
- 使用 Ruby 目录:
Dir 类对 Ruby 目录操作使用不同的方法,例如new()、pwd()、home()、path()、getwd()、chdir()、entry()、glob()等。- new() 用于创建一个新的目录对象。
句法:
- new() 用于创建一个新的目录对象。
obj=Dir.new("name_of_directory")
- 在上面的代码中,文件夹目录应该已经存在。
- Dir 类中的pwd()方法返回当前目录。
句法:
Dir.pwd
- 例子:
红宝石
# creating directory
Dir.mkdir("folder")
# returns current working directory
puts Dir.pwd
- 输出:
/workspace
- Dir 类中的home()方法用于返回当前用户的主目录。
句法:
Dir.home
- 例子:
红宝石
# creating directory
Dir.mkdir("folder")
# returns home directory
puts Dir.home
- 输出:
/workspace
- 下面的代码将返回特定用户的主目录。
Dir.home('username')
- Dir 类的path()方法用于返回路径参数。
句法:
d=Dir.new("name_of_directory")
d.path
- 例子:
红宝石
# creating directory
Dir.mkdir("folder")
# creating object of that directory using new() method
obj=Dir.new("folder")
# assigns the path parameter of obj to variable f
f=obj.path
print("#{f}")
- 输出:
folder
- Dir 类的getwd()方法用于返回当前目录的路径。
句法:
Dir.getwd
- 例子:
红宝石
# creating directory
Dir.mkdir("folder")
# returns the path of the current working directory
puts Dir.getwd
- 输出:
/workspace
- Dir 类的chdir()方法用于修改当前目录。
句法:
Dir.chdir("name_of_directory")
- 例子:
红宝石
# creating directories
Dir.mkdir("/workspace/folder1")
Dir.mkdir("/workspace/folder2")
# displaying the path of the current directory
puts Dir.pwd
# changing the current working directory
Dir.chdir("folder2")
puts Dir.pwd
- 输出:
/workspace
/workspace/folder2
- Dir 类中的entries()方法用于以数组的形式返回目录中存在的所有文件和文件夹。
句法:
Dir.entries("directory")
- 例子:
红宝石
# creating a directory named folder
Dir.mkdir("folder")
# displaying the path of the current directory
puts Dir.pwd
# changing current working directory to folder
Dir.chdir("folder")
puts Dir.pwd
# creating directories inside folder
Dir.mkdir("subfolder1")
Dir.mkdir("subfolder2")
Dir.mkdir("subfolder3")
# displays all the files and folders present in folder
print("Entries:\n")
puts Dir.entries("C:/Users/KIIT/Desktop/folder")
- 输出:
C:/Users/KIIT/Desktop
C:/Users/KIIT/Desktop/folder
Entries:
.
..
subfolder1
subfolder2
subfolder3
- Dir 类中的glob()方法用于显示所有具有某种匹配模式的文件。
句法:
Dir.glob("pattern")
- 例子:
红宝石
# creating a directory named folder
Dir.mkdir("folder")
# changing current working directory to folder
Dir.chdir("folder")
# creating directories inside folder
Dir.mkdir("dabce")
Dir.mkdir("abcd")
Dir.mkdir("program.rb")
Dir.mkdir("program2.rb")
# displaying specified files and folders
print"\nAll files in the current working directory: \n"
puts Dir.glob("*")
print"\nAll files containing 'abc' in the name: \n"
puts Dir.glob("*abc*")
print"\nAll ruby files: \n"
puts Dir.glob("*.rb")
- 输出:
All files in the current working directory:
abcd
dabce
program.rb
program2.rb
All files containing 'abc' in the name:
abcd
dabce
All ruby files:
program.rb
program2.rb
- 删除 Ruby 目录:
Dir 类中有多种方法可以删除 Ruby 目录,例如rmdir()、delete()和unlink()
句法:
Dir.delete "folder"
Dir.rmdir "folder"
Dir.unlink "folder"
- 例子:
红宝石
#creating directory
Dir.mkdir("folder")
puts Dir.exist?("folder")
# deleting directory
Dir.rmdir("folder")
puts Dir.exist?("folder")
- 输出:
true
false
- 创建嵌套目录:
FileUtils 模块中的 mkdir_p()方法用于创建目录及其所有父目录。
句法:
FileUtils.mkdir_p 'directory_path'
- 例子:
红宝石
# creating directory parent_folder
Dir.mkdir "parent_folder"
print("Current Directory: ")
puts Dir.pwd
require "fileutils"
# creating nested directory in parent_folder
FileUtils.mkdir_p "parent_folder/child_folder/folder"
# changing current directory to parent_folder
Dir.chdir("/workspace/parent_folder")
print("Current Directory: ")
puts Dir.pwd
# checking child folder exists or not
puts Dir.exists?("child_folder")
# changing current directory to child_folder
Dir.chdir("/workspace/parent_folder/child_folder")
print("Current Directory: ")
puts Dir.pwd
# checking folder exists or not
puts Dir.exists?("folder")
- 输出:
Current Directory: /workspace
Current Directory: /workspace/parent_folder
true
Current Directory: /workspace/parent_folder/child_folder
true
- 移动文件和文件夹:
FileUtils 模块中的mv()和move()方法用于将文件和文件夹从当前目录移动到目标目录。
句法:
FileUtils.mv("source", "destination")
- 例子:
红宝石
# creating directories
Dir.mkdir "folder1"
Dir.mkdir "folder2"
require "fileutils"
# moving directory folder1 into directory folder2
FileUtils.mv( "folder1", "folder2")
# changing current directory to folder2
Dir.chdir("folder2")
# checking if folder1 exists in folder 2
puts Dir.exists?("folder1")
- 输出:
true
- 将文件从一个目录复制到另一个目录:
FileUtils 模块中的cp()方法用于将文件从当前目录复制到目标目录。
句法:
FileUtils.cp("source", "destination")
- 例子:
考虑两个目录 folder1 和 folder2 已经创建并且 folder2 包含 test.txt 。
红宝石
require "fileutils"
# copying test.txt from folder1 to folder2
FileUtils.cp( "folder2/test.txt", "folder1")
Dir.chdir("folder1")
# checking if test.txt exists in folder1
puts File.exist?("test.txt")
- 输出:
true