How to split a directory string in Ruby?

Viewed 17771

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]
5 Answers

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"

Related