Reputation: 81570
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]
Upvotes: 25
Views: 19540
Reputation: 3916
You want:
File.split( "/home/gumby/bigproject/now_with_bugs_fixed/32/FOO_BAR_2096.results").first
=> "/home/gumby/bigproject/now_with_bugs_fixed/32"
Upvotes: 0
Reputation: 48755
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_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
.
Source: https://ruby-doc.org/stdlib-2.3.3/libdoc/pathname/rdoc/Pathname.html
Upvotes: 46
Reputation: 48755
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 /
.
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.
Upvotes: 1
Reputation: 9026
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] 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:
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>
Upvotes: 7
Reputation: 27591
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]
Upvotes: 26