I have a string like:
str = "/some/path/to/some/file.ext"
The result should be like:
[path, dir, file]
=> ["/some/path/to", "some", "file.ext"]
My current code:
chunks = str.split '/'
=> ["", "some", "path", "to", "some", "file.ext"]
file = chunks.pop
=> "file.ext"
dir = chunks.pop
=> "some"
path = chunks.join '/'
=> "/some/path/to"
But it’s ugly and slow.
I also tried regular expressions and File.split, but I got an even uglier mess.
What is the solution?
Use
pathname:It runs great on all versions.
Here you can see it in action.