Ruby, getting path from path+filename

RubyDirectory

Ruby Problem Overview


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.

Ruby Solutions


Solution 1 - Ruby

Use the Ruby File.dirname method.

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

Solution 2 - Ruby

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionOnetimeposter123View Question on Stackoverflow
Solution 1 - RubySimone CarlettiView Answer on Stackoverflow
Solution 2 - RubyTwonkyView Answer on Stackoverflow