slash and backslash in Ruby

后端 未结 5 717
北荒
北荒 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

    Yes, it's annoying as a windows users to keep replacing those backslashes to slashes and vice-versa if you need the path to copy it to your filemanager, so i do it like his. It does no harm if you are on Linux or Mac and saves a lot of nuisance in windows.

    path = 'I:\ebooks\dutch\_verdelen\Komma'.gsub(/\\/,'/')
    
    Dir.glob("#{path}/**/*.epub").each do |file|
        puts file
    end
    

提交回复
热议问题