Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get parent directory of current directory in Ruby

Tags:

directory

ruby

I understand I can get current directory by

$CurrentDir = Dir.pwd 

How about parent directory of current directory?

like image 603
icn Avatar asked Dec 28 '11 20:12

icn


People also ask

How do I get the current directory in Ruby?

pwd : To check the current working directory, pwd(present working directory) method is used. 5. chdir : To change the current working directory, chdir method is used.

How do I find parent directory name?

The dirname() function takes a pointer to a character string that contains a path name, and returns a pointer to a string that is a path name of the parent directory of that file. Trailing '/' characters in the path are not counted as part of the path.

What is __ DIR __ in Ruby?

__dir__ is a alias to a function As the ruby documentation says __dir__ is equal to File.dirname(File.realpath(__FILE__))

What is current directory and parent directory?

The root directory contains all other folders and files. Every directory, except the root directory, lies beneath another directory. The directory containing the current directory is called the parent directory, and the directory located within the current directory is called a subdirectory.


2 Answers

File.expand_path("..", Dir.pwd) 
like image 90
Rob Di Marco Avatar answered Oct 27 '22 22:10

Rob Di Marco


Perhaps the simplest solution:

puts File.expand_path('../.')  
like image 25
Marek Příhoda Avatar answered Oct 27 '22 21:10

Marek Příhoda