How to Get the Current Working Directory's Absolute Path from Irb

How to get the current working directory's absolute path in Ruby?

Dir.pwd is the current working directory

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

Get the file path based on the current working directory?

You should be able to just do something like follows.

absolute_path.split(Dir.pwd.to_s)[1].sub('/','')

This gets a 2 element array where the second element is the remaining portion of the path you want. The sub then strips off the prefixing forward slash.

Why does irb -r make __FILE__ an absolute path?

The reason this happens in IRB is the same reason this would happen if you had written a program named bar.rb with the following contents.

require './foo'

You will find that using IRB with the require as you do is no different than calling bar.rb which has the require.

From the documentation:

__FILE__ -- The name of the file currently being executed, including
path relative to the directory where the application was started up
(or the current directory, if it has been changed).

So this is including the path relative to the directory where the application was started up. Who knows where irb or bar are? When it is not clear, then the path is given as well.

How do I find the absolute path of the currently running Ruby script?

This can be done simply using:

File.expand_path $0

Ruby: How to get current main project working directory's

This will give you the path to the current file.

__FILE__

In order to expand a relative path, do this:

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

Why isn't current directory on my Ruby path?

In Ruby 1.9.2 the Powers that Be introduced an explicit change so that the working directory is no longer in the Ruby path. I thought it was the Apocalypse and a terrible thing, until I learned about require_relative. My apps tend to look like this:

require 'some_gem'
require 'another_gem'
require_relative 'lib/init'

And then lib/init.rb can have:

require_relative 'lib1' # this is lib/lib1.rb
require_relative 'lib2' # this is lib/lib2.rb

It's the bees knees, and solves all sorts of problems I used to have with requiring the same file from different working directories.

Edit: Unfortunately (for reasons I don't know and haven't looked into) require_relative doesn't work specifically in irb. For this you can:

  1. do what you initially described: either $: << '.' or $:.unshift '.', or
  2. you can use load 'myfile.rb' or require './myfile' instead:

    irb(main):001:0> Dir['*.rb']
    => ["a.rb", "bar.rb", "foo.rb", "prime.rb", "tmp.rb"]

    irb(main):002:0> require 'a'
    LoadError: no such file to load -- a
    from <internal:lib/rubygems/custom_require>:29:in `require'
    from <internal:lib/rubygems/custom_require>:29:in `require'
    from (irb):2
    from /usr/local/bin/irb:12:in `<main>'

    irb(main):003:0> require_relative 'a'
    LoadError: cannot infer basepath
    from (irb):3:in `require_relative'
    from (irb):3
    from /usr/local/bin/irb:12:in `<main>'

    irb(main):004:0> load 'a.rb'
    a
    => true

    irb(main):005:0> require './a'
    a
    => true


Related Topics



Leave a reply



Submit