How to split a directory string in Ruby?
The correct answer is to use Ruby's Pathname
(in-built class since 1.8.7, not a gem).
See the code:
require 'pathname'def split_path(path) Pathname(path).each_filename.to_aend
Doing this will discard the information whether the path was absolute or relative. To detect this, you can call absolute?
method on Pathname
.
Source: https://ruby-doc.org/stdlib-2.3.3/libdoc/pathname/rdoc/Pathname.html
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.
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]
Rake provides a split_all method added to FileUtils. It's pretty simple and uses 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]endtaken 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:
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>