Ruby, getting path from path+filename
Programming language: Ruby 1.9
Problem String: C:/Test/blah.txt
to C:/Test/
I know it's an easy question, but Google and the Ruby quickref for File
have no solution for me.
And I have no experience with Regex.
Solution 1:
Use the Ruby File.dirname
method.
File.dirname("C:/Test/blah.txt")
# => "C:/Test"
Solution 2:
More versatile would be the Ruby Pathname class:
require 'pathname'
pn = Pathname.new("C:/Test/blah.txt")
p pn.dirname.to_s + Pathname::SEPARATOR_LIST
which gives C:/Test/
.