windows 如何在Ruby中拆分目录字符串?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1370988/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 13:05:06  来源:igfitidea点击:

How to split a directory string in Ruby?

rubylinuxwindowsstring

提问by Andrew Grimm

In ruby, I'm able to do

在红宝石中,我能够做到

File.dirname("/home/gumby/bigproject/now_with_bugs_fixed/32/FOO_BAR_2096.results")

and get

并得到

"/home/gumby/bigproject/now_with_bugs_fixed/32"

but now I'd like to split up that directory string into the individual folder components, ie something like

但现在我想将该目录字符串拆分为各个文件夹组件,即类似于

["home", "gumby", "bigproject", "now_with_bugs_fixed", "32"]

Is there a way to do that other than using

除了使用之外,还有没有办法做到这一点

directory.split("/")[1:-1]

回答by Rok Kralj

The correct answer is to use Ruby's Pathname(in-built class since 1.8.7, not a gem).

正确的答案是使用 Ruby Pathname(自 1.8.7 以来的内置类,而不是 gem)。

See the code:

看代码:

require 'pathname'

def split_path(path)
    Pathname(path).each_filename.to_a
end

Doing this will discard the information whether the path was absolute or relative. To detect this, you can call absolute?method on Pathname.

这样做将丢弃路径是绝对路径还是相对路径的信息。要检测到这一点,您可以absolute?在 上调用方法Pathname

Source: https://ruby-doc.org/stdlib-2.3.3/libdoc/pathname/rdoc/Pathname.html

来源:https: //ruby-doc.org/stdlib-2.3.3/libdoc/pathname/rdoc/Pathname.html

回答by Rudd Zwolinski

There's no built-in function to split a path into its component directories like there is to join them, but you can try to fake it in a cross-platform way:

没有内置函数将路径拆分为其组件目录,就像加入它们一样,但您可以尝试以跨平台的方式伪造它:

directory_string.split(File::SEPARATOR)

This works with relative paths and on non-Unix platforms, but for a path that starts with "/"as the root directory, then you'll get an empty string as your first element in the array, and we'd want "/"instead.

这适用于相对路径和非 Unix 平台,但对于以"/"根目录开头的路径,您将获得一个空字符串作为数组中的第一个元素,而我们想要的是"/"

directory_string.split(File::SEPARATOR).map {|x| x=="" ? File::SEPARATOR : x}

If you want just the directories without the root directory like you mentioned above, then you can change it to select from the first element on.

如果您只想要像上面提到的那样没有根目录的目录,那么您可以将其更改为从第一个元素开始选择。

directory_string.split(File::SEPARATOR).map {|x| x=="" ? File::SEPARATOR : x}[1..-1]

回答by James Moore

Rake provides a split_all method added to FileUtils. It's pretty simple and uses File.split:

Rake 提供了一个 split_all 方法添加到 FileUtils。它非常简单并使用 File.split:

def split_all(path)
  head, tail = File.split(path)
  return [tail] if head == '.' || tail == '/'
  return [head, tail] if head == '/'
  return split_all(head) + [tail]
end

taken from rake-0.9.2/lib/rake/file_utils.rb

The rake version has slightly different output from Rudd's code. Rake's version ignores multiple slashes:

rake 版本的输出与 Rudd 的代码略有不同。Rake 的版本忽略了多个斜杠:

irb(main):014:0> directory_string = "/foo/bar///../fn"
=> "/foo/bar///../fn"
irb(main):015:0> directory_string.split(File::SEPARATOR).map {|x| x=="" ? File::SEPARATOR : x}[1..-1]
=> ["foo", "bar", "/", "/", "..", "fn"]
irb(main):016:0> split_all directory_string
=> ["/", "foo", "bar", "..", "fn"]
irb(main):017:0>

回答by Rok Kralj

Warning: This solution is no longer the best one. See my other one.

警告:此解决方案不再是最好的解决方案。看我的另一个。

Actually, there is a more neat solution. The main idea is to keep popping the basename until you are only left with the .or /.

实际上,还有一个更巧妙的解决方案。主要思想是不断弹出基本名称,直到只剩下.or为止/

def split_path(path)
    array = []
    until ['/', '.'].include? path
        array << File.basename(path)
        path = File.dirname(path)
    end
    array.reverse
end

split_path 'a/b/c/d' #=> ['a', 'b', 'c', 'd']

You can further build upon this idea, if you wish.

如果你愿意,你可以进一步建立在这个想法的基础上。