[ruby] Ruby, 경로 + 파일 이름에서 경로 가져 오기

프로그래밍 언어 : Ruby 1.9

문제 문자열 : C:/Test/blah.txt
toC:/Test/

쉬운 질문이라는 것을 알고 있지만 Google과 Ruby 빠른 참조 File에는 해결책이 없습니다.
그리고 저는 Regex에 대한 경험이 없습니다.



답변

Ruby File.dirname방법을 사용하십시오 .

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


답변

더 다재다능한 것은 Ruby Pathname 클래스입니다.

require 'pathname'

pn = Pathname.new("C:/Test/blah.txt")
p pn.dirname.to_s + Pathname::SEPARATOR_LIST

제공합니다 C:/Test/.


답변