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.
Pass a string to File. expand_path to generate the path to that file or directory. Relative paths will reference your current working directory, and paths prepended with ~ will use the owner's home directory.
Use the Ruby File. dirname method. For me, File. dirname("/a/b/c/d") correctly returns /a/b/c but File.
In Ruby, the Windows version anyways, I just checked and __FILE__ does not contain the full path to the file. Instead it contains the path to the file relative to where it's being executed from.
Use the Ruby File.dirname
method.
File.dirname("C:/Test/blah.txt") # => "C:/Test"
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/
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With