Ruby 带有反斜杠的路径 Mac

Ruby paths with backslash on Mac

冒险进入 Ruby 土地(学习 Ruby)。我喜欢,有趣的编程语言。

无论如何,我正在尝试构建一个简单的程序来从文件夹中删除后缀,其中用户在 Mac 终端中提供文件夹的路径。

场景是这样的:

  1. 用户运行我的程序
  2. 程序要求用户输入文件夹路径
  3. 用户将文件夹拖放到 Mac 终端
  4. 程序接收路径如“/Users/zhang/Desktop/test\文件夹
  5. 程序开始并将该文件夹中的所有文件重命名为 "image_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 浏览了这个学习资源:

http://rubylearning.com/satishtalim/tutorial.html

我也在使用这些资源来找出我做错了什么:

http://ruby-doc.org/core-2.3.0/Dir.html

https://robm.me.uk/ruby/2014/01/18/pathname.html

有什么想法吗?

编辑

好吧,当前的解决方法(?)是使用新方法清理我的原始字符串并删除所有反斜杠:

def cleanBackslash(originalString)
  return originalString.gsub("\", "")
end

然后

def ask_for_directory_path
  puts "\nWhat is the path to the drawable folder you need cleaning?:"
  rawPath = gets.chomp.strip
  rawPath = cleanBackslash(rawPath)

  ...

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 显示的转义字符,以防止 space 被解释为分隔符。

就像 Ruby 通过转义双引号来显示包含双引号的字符串。

好吧,首先,使用 gets.chomp.strip 可能不是一个好主意 :P

与您通常在真实 bash 程序中看到的更好更接近的解决方案是使用 Readline 库:

require "Readline"

...

def ask_for_directory_path
  rawPath = String.new

  rawPath = Readline.readline("\nWhat is the path to the drawable folder you need cleaning?\n> ", true)
  rawPath = rawPath.chomp.strip
  rawPath = cleanBackslash(rawPath)

  @directoryPath = Pathname.new(rawPath)
end

使用 Readline 可以让您按 Tab 键完成文件夹路径。我还需要使用自己定义的 readline 清除反斜杠:

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 程序哈哈。

最终计划

查看:

我现在感觉自己像个老板! :D