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__)
我在Windows上运行Ruby,但我不知道这是否会有所不同。我要做的就是获取当前工作目录的绝对路径。这能从irb得到吗?显然,从脚本中可以使用File.expand_path(__FILE__)
But from irb I tried the following and got a "Permission denied" error:
但从irb我尝试了以下,并得到了一个“拒绝许可”错误:
File.new(Dir.new(".").path).expand
6 个解决方案
#1
423
Dir.pwd
seems to do the trick.
Dir。pwd似乎做到了这一点。
http://ruby-doc.org/core/Dir.html#method-c-pwd
http://ruby-doc.org/core/Dir.html method-c-pwd
#2
162
File.expand_path File.dirname(__FILE__)
will return the directory relative to the file this command is called from.
文件。expand_path file .dirname(__FILE__)将返回与此命令调用的文件相关的目录。
But Dir.pwd
returns the working directory (results identical to executing pwd
in your terminal)
但Dir。pwd返回工作目录(结果与在终端中执行pwd相同)
#3
46
As for the path relative to the current executing script, since Ruby 2.0 you can also use
至于相对于当前执行脚本的路径,因为Ruby 2.0也可以使用。
__dir__
So this is basically the same as
这和
File.dirname(__FILE__)
#4
4
This will give you the working directory of the current file.
这将为您提供当前文件的工作目录。
File.dirname(__FILE__)
Example:
例子:
current_file: "/Users/nemrow/SITM/folder1/folder2/amazon.rb"
current_file:“/用户/ nemrow / SITM / folder1 / folder2 / amazon.rb”
result: "/Users/nemrow/SITM/folder1/folder2"
结果:“/用户/ nemrow / SITM / folder1 / folder2”
#5
2
If you want to get the full path of the directory of the current rb file:
如果您想获取当前rb文件目录的完整路径:
File.expand_path('../', __FILE__)
#6
1
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"
#1
423
Dir.pwd
seems to do the trick.
Dir。pwd似乎做到了这一点。
http://ruby-doc.org/core/Dir.html#method-c-pwd
http://ruby-doc.org/core/Dir.html method-c-pwd
#2
162
File.expand_path File.dirname(__FILE__)
will return the directory relative to the file this command is called from.
文件。expand_path file .dirname(__FILE__)将返回与此命令调用的文件相关的目录。
But Dir.pwd
returns the working directory (results identical to executing pwd
in your terminal)
但Dir。pwd返回工作目录(结果与在终端中执行pwd相同)
#3
46
As for the path relative to the current executing script, since Ruby 2.0 you can also use
至于相对于当前执行脚本的路径,因为Ruby 2.0也可以使用。
__dir__
So this is basically the same as
这和
File.dirname(__FILE__)
#4
4
This will give you the working directory of the current file.
这将为您提供当前文件的工作目录。
File.dirname(__FILE__)
Example:
例子:
current_file: "/Users/nemrow/SITM/folder1/folder2/amazon.rb"
current_file:“/用户/ nemrow / SITM / folder1 / folder2 / amazon.rb”
result: "/Users/nemrow/SITM/folder1/folder2"
结果:“/用户/ nemrow / SITM / folder1 / folder2”
#5
2
If you want to get the full path of the directory of the current rb file:
如果您想获取当前rb文件目录的完整路径:
File.expand_path('../', __FILE__)
#6
1
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"