how to get the current working directory's absolute path from irb

RubyIrbWorking Directory

Ruby Problem Overview


I'm running Ruby on Windows though I don't know if that should make a difference. All I want to do is get the current working directory's absolute path. Is this possible from irb? Apparently from a script it's possible using File.expand_path(__FILE__)

But from irb I tried the following and got a "Permission denied" error:

File.new(Dir.new(".").path).expand

Ruby Solutions


Solution 1 - Ruby

Dir.pwd seems to do the trick.

http://ruby-doc.org/core/Dir.html#method-c-pwd

Solution 2 - Ruby

File.expand_path File.dirname(__FILE__) will return the directory relative to the file this command is called from.

But Dir.pwd returns the working directory (results identical to executing pwd in your terminal)

Solution 3 - Ruby

As for the path relative to the current executing script, since Ruby 2.0 you can also use

__dir__

So this is basically the same as

File.dirname(__FILE__)

Solution 4 - Ruby

This will give you the working directory of the current file.

File.dirname(__FILE__)

Example:

current_file: "/Users/nemrow/SITM/folder1/folder2/amazon.rb"

result: "/Users/nemrow/SITM/folder1/folder2"

Solution 5 - Ruby

Through this you can get absolute path of any file located in any directory.

File.join(Dir.pwd,'some-dir','some-file-name')

This will return

=> "/User/abc/xyz/some-dir/some-file-name"

Solution 6 - Ruby

If you want to get the full path of the directory of the current rb file:

File.expand_path('../', __FILE__)

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
QuestionDexygenView Question on Stackoverflow
Solution 1 - Rubyuser85509View Answer on Stackoverflow
Solution 2 - RubyPsyloneView Answer on Stackoverflow
Solution 3 - RubyudoView Answer on Stackoverflow
Solution 4 - RubyJordan Rumpelstiltskin NemrowView Answer on Stackoverflow
Solution 5 - Rubyyeshwant singhView Answer on Stackoverflow
Solution 6 - RubyChâu Hồng LĩnhView Answer on Stackoverflow