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

后端 未结 6 1818
一向
一向 2021-01-29 18:54

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 fr

相关标签:
6条回答
  • 2021-01-29 19:02

    Dir.pwd seems to do the trick.

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

    0 讨论(0)
  • 2021-01-29 19:04

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

    File.expand_path('../', __FILE__)
    
    0 讨论(0)
  • 2021-01-29 19:05

    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__)
    
    0 讨论(0)
  • 2021-01-29 19:05

    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"
    
    0 讨论(0)
  • 2021-01-29 19:11

    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"

    0 讨论(0)
  • 2021-01-29 19:13

    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)

    0 讨论(0)
提交回复
热议问题