Mac上带反斜杠的Ruby路径
冒险进入Ruby领域(学习Ruby)。我喜欢它,有趣的编程语言 无论如何,我正在尝试构建一个简单的程序来删除文件夹中的后缀,用户在Mac终端中提供文件夹的路径 场景如下所示:Mac上带反斜杠的Ruby路径,ruby,path,dir,Ruby,Path,Dir,冒险进入Ruby领域(学习Ruby)。我喜欢它,有趣的编程语言 无论如何,我正在尝试构建一个简单的程序来删除文件夹中的后缀,用户在Mac终端中提供文件夹的路径 场景如下所示: 用户运行我的程序 程序要求用户输入文件夹路径 用户将文件夹拖放到Mac终端 程序接收路径,如“/Users/zhang/Desktop/test\folder” 该程序将使用后缀“image\u mdpi.png”将该文件夹中的所有文件重命名为“image.png” 不过我遇到了一个问题 现在,我正在尝试使用以下命令列出目
Dir.entries(@directoryPath)
然而,Dir.entries似乎不喜欢路径中的反斜杠“\”。如果我对带有反斜杠的路径使用Dir.entries()
因此,我的下一个想法是使用:
Pathname.new(rawPath)
让Ruby创建一个正确的路径。不幸的是,即使是Pathname.new()
也不喜欢反斜杠。我的候机楼在吐口水
@directoryPath is not dir
这是我到目前为止的源代码:
# ------------------------------------------------------------------------------
# Renamer.rb
# ------------------------------------------------------------------------------
#
# Program to strip out Android suffixes like _xhdpi, _hpdi, _mdpi and _ldpi
# but only for Mac at the moment.
#
# --------------------------------------------------
# Usage:
# --------------------------------------------------
# 1. User enters a the path to the drawable folder to clean
# 2. program outputs list of files and folder it detects to clean
# 3. program ask user to confirm cleaning
require "Pathname"
@directoryPath = ''
@isCorrectPath = false
# --------------------------------------------------
# Method definitions
# --------------------------------------------------
def ask_for_directory_path
puts "What is the path to the drawable folder you need cleaning?:"
rawPath = gets.chomp.strip
path = Pathname.new("#{rawPath}")
puts "Stored dir path = '#{path}'"
if path.directory?
puts "@directoryPath is dir"
else
puts "@directoryPath is not dir"
end
@directoryPath = path.to_path
end
def confirm_input_correct
print "\n\nIs this correct? [y/N]: "
@isCorrectPath = gets.chomp.strip
end
def reconfirm_input_correct
print "please enter 'y' or 'N': "
@isCorrectPath = gets.strip
end
def output_folder_path
puts "The folder '#{@directoryPath}' contains the following files and folders:"
# Dir.entries doesn't like \
# @directoryPath = @directoryPath.gsub("\\", "")
puts "cleaned path is '#{@directoryPath}'"
begin
puts Dir.entries(@directoryPath)
rescue
puts "\n\nLooks like the path is incorrect:"
puts @directoryPath
end
end
def clean_directory
puts "Cleaning directory now..."
end
puts "Hello, welcome to Renamer commander.\n\n"
ask_for_directory_path
output_folder_path
confirm_input_correct
while @isCorrectPath != 'y' && @isCorrectPath != 'N' do
reconfirm_input_correct
end
if @isCorrectPath == 'y'
clean_directory
else
ask_for_directory_path
end
三天前,我浏览了Ruby 2的学习资源:
我还利用这些资源来找出我做错了什么:
有什么想法吗
编辑
嗯,当前的解决方法(?)是使用新方法清理原始字符串并删除任何反斜杠:
def cleanBackslash(originalString)
return originalString.gsub("\\", "")
end
然后
我想不是最漂亮的
程序的示例运行:
Zhang-computer:$ ruby Renamer.rb
Hello, welcome to Renamer commander.
What is the path to the drawable folder you need cleaning?:
/Users/zhang/Desktop/test\ folder
Stored dir path = '/Users/zhang/Desktop/test folder'
@directoryPath is dir
The folder '/Users/zhang/Desktop/test folder' contains the following files and folders:
cleaned path is '/Users/zhang/Desktop/test folder'
.
..
.DS_Store
file1.txt
file2.txt
file3.txt
Is this correct? [y/N]:
:]我认为问题不在于反斜杠,而在于空格。你不需要逃避它:
Dir.pwd
# => "/home/lbrito/work/snippets/test folder"
Dir.entries(Dir.pwd)
# => ["..", "."]
尝试调用Dir.entries而不转义空格。路径中没有反斜杠。反斜杠是shell显示的转义字符,用于防止将空格解释为分隔符
就像Ruby通过转义双引号来显示包含双引号的字符串一样。好的,首先,使用get.chomp.strip
可能不是一个好主意:p
与您通常在真实bash程序中看到的情况相比,更好、更接近的解决方案是使用Readline库:
i、 e
使用Readline可以通过tab键完成文件夹路径。我还需要使用我自己定义的:
def cleanBackslash(originalString)
return originalString.gsub("\\", "")
end
之后,Dir.entries(@directorPath)可以列出路径中的所有文件和文件夹,无论用户是手动键入还是将文件夹拖放到Mac终端:
Zhang-iMac:Renamer zhang$ ruby Renamer.rb
Hello, welcome to Renamer commander.
What is the path to the drawable folder you need cleaning?
> /Users/zhang/Ruby\ Learning/Renamer/test_folder
The folder '/Users/zhang/Ruby Learning/Renamer/test_folder' contains the following files and folders:
.
..
.DS_Store
drawable
drawable-hdpi
drawable-mdpi
drawable-xhdpi
drawable-xxhdpi
drawable-xxxhdpi
Is this correct? [y/N]: y
Cleaning directory now...
程序还没有完成,但我想这解决了我的反斜杠的问题
我不知道真正的BASH程序是如何制作的,但请考虑一下这是我可怜的人的BASH程序LOL.<
最终计划
请查看:
我现在感觉自己像个老板了!:D当我将文件夹拖放到终端时(当我的程序正在运行并等待用户输入时),生成的字符串包含反斜杠和空格。我不相信它与使用bash shell时完成的普通选项卡的转义字符是一样的。如果我在不拖放的情况下手动键入路径,并且省略了反斜杠,Dir.entries(@directoryPath)
确实有效,但当我使用以下命令手动硬编码时:Dir.entries(“/Users/zhang/Desktop/test\folder”)
出于某种奇怪的原因,它也能起作用。当@directoryPath包含“\”并且我没有硬编码它时,它失败了。它看起来像是rawPath=gets.chomp.strip
按照可视的方式存储字符串,而不是转义它。因此,当用户进入test\folder
时,会获取.chomp.strip
字符串test\folder
,并包含反斜杠和空格:D
def cleanBackslash(originalString)
return originalString.gsub("\\", "")
end
Zhang-iMac:Renamer zhang$ ruby Renamer.rb
Hello, welcome to Renamer commander.
What is the path to the drawable folder you need cleaning?
> /Users/zhang/Ruby\ Learning/Renamer/test_folder
The folder '/Users/zhang/Ruby Learning/Renamer/test_folder' contains the following files and folders:
.
..
.DS_Store
drawable
drawable-hdpi
drawable-mdpi
drawable-xhdpi
drawable-xxhdpi
drawable-xxxhdpi
Is this correct? [y/N]: y
Cleaning directory now...