在Mac上使用反斜杠的Ruby路径

时间:2016-02-26 17:12:44

标签: ruby path dir

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

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

场景如下:

  1. 用户运行我的程序
  2. 程序要求用户输入文件夹路径
  3. 用户将文件夹拖放到Mac终端
  4. 程序接收路径,例如“ / Users / zhang / Desktop / test \ folder
  5. 程序将该文件夹中的所有文件重命名为后缀,例如“image _mdpi .png”至“image.png”
  6. 我遇到了一个问题。

    现在,我正在尝试使用以下方式列出目录的内容:

    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]: 
    

3 个答案:

答案 0 :(得分:0)

我认为问题不在于反斜杠,而在于空白。你不需要逃避它:

Dir.pwd
# => "/home/lbrito/work/snippets/test folder"
Dir.entries(Dir.pwd)
# => ["..", "."]

尝试在不转义空格的情况下调用Dir.entries。

答案 1 :(得分:0)

路径中没有反斜杠。反斜杠是shell显示的转义字符,用于防止将空间解释为分隔符。

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

答案 2 :(得分:0)

好的,首先,使用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可以标签填写文件夹路径。我还需要使用我自己的定义来清除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程序大声笑。

最终节目

检查出来:

demo run screenshot

我现在觉得自己像个老板! :d