本文介绍了Ruby中的斜杠和反斜杠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想编写一个可以在Windows和Linux上运行的应用程序.但是我遇到了路径问题,因为Windows使用"\",而Linux使用"/".如何解决此问题.谢谢
I want to write a application that works in windows and linux. but I have a path problem because windows use "\" and Linux use "/" .how can I solve this problem.thanks
推荐答案
在Ruby中,Linux或Windows中的路径没有区别.无论环境如何,路径都应使用/
.因此,要在Windows中使用任何路径,请将所有\
替换为/
. File#join
适用于Windows和Linux.例如,在Windows中:
In Ruby, there is no difference between the paths in Linux or Windows. The path should be using /
regardless of environment. So, for using any path in Windows, replace all \
with /
. File#join
will work for both Windows and Linux. For example, in Windows:
Dir.pwd
=> "C/Documents and Settings/Users/prince"
File.open(Dir.pwd + "/Desktop/file.txt", "r")
=> #<File...>
File.open(File.join(Dir.pwd, "Desktop", "file.txt"), "r")
=> #<File...>
File.join(Dir.pwd, "Desktop", "file.txt")
=> "C/Documents and Settings/Users/prince/Desktop/file.txt"
这篇关于Ruby中的斜杠和反斜杠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!