Onetimeposter123
Onetimeposter123

Reputation: 1105

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.

Upvotes: 105

Views: 72109

Answers (2)

Twonky
Twonky

Reputation: 806

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/.

Upvotes: 2

Simone Carletti
Simone Carletti

Reputation: 176382

Use the Ruby File.dirname method.

File.dirname("C:/Test/blah.txt")
# => "C:/Test" 

Upvotes: 192

Related Questions