slash and backslash in Ruby

后端 未结 5 737
北荒
北荒 2021-01-04 01:16

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

5条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-04 02:11

    Use the Pathname class to generate paths which then will be correct on your system:

    a_path = Pathname.new("a_path_goes_here")
    

    The benefit of this is that it will allow you to chain directories by using the + operator:

    a_path + "another_path" + "and another"
    

    Calling a_path.to_s will then generate the correct path for the system that you are on.

提交回复
热议问题