반응형
Ruby에서 현재 디렉토리의 상위 디렉토리 가져 오기
나는 내가 현재 디렉토리를 얻을 수 있음을 이해한다.
$CurrentDir = Dir.pwd
현재 디렉토리의 상위 디렉토리는 어떻습니까?
File.expand_path("..", Dir.pwd)
아마도 가장 간단한 해결책은 다음과 같습니다.
puts File.expand_path('../.')
더 간단한 해결책은 다음을 사용하는 것입니다 File.dirname
.
2.3.0 :005 > Dir.pwd
=> "/Users/kbennett/temp"
2.3.0 :006 > File.dirname(Dir.pwd)
=> "/Users/kbennett"
2.3.0 :007 > File.basename(Dir.pwd)
=> "temp"
File.basename
File.dirname
그렇지 않은 경로의 구성 요소를 반환합니다 .
물론 이것은 filespec이 절대적이고 상대적이 아닌 경우에만 작동합니다. 절대적으로 만들려면 다음과 같이 할 수 있습니다.
2.3.0 :008 > File.expand_path('.')
=> "/Users/kbennett/temp"
2.3.0 :009 > File.dirname(File.expand_path('.'))
=> "/Users/kbennett"
참조 URL : https://stackoverflow.com/questions/8660732/get-parent-directory-of-current-directory-in-ruby
반응형
'developer tip' 카테고리의 다른 글
NodeJS / Express에서“module.exports”와“exports.methods”는 무엇을 의미합니까? (0) | 2020.12.29 |
---|---|
MySQL Workbench :“ON UPDATE”및 CURRENT_TIMESTAMP를 어떻게 설정합니까? (0) | 2020.12.29 |
Eclipse의 Android 기기에 Nexus7이 표시되지 않음 (0) | 2020.12.29 |
jQuery 및 Twitter Bootstrap을 사용하여 활성 탭 찾기 (0) | 2020.12.29 |
프로그래밍 방식으로 Android에서 카메라로 사진 촬영 (0) | 2020.12.29 |