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

The best answers to the question “how to get the current working directory's absolute path from irb” in the category Dev.

QUESTION:

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

ANSWER:

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)

ANSWER:

Dir.pwd seems to do the trick.

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

ANSWER:

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”

ANSWER:

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__)