How to do a safe join pathname in ruby?

后端 未结 2 953
长发绾君心
长发绾君心 2020-12-07 15:10

My Rails development environment is Windows-based, and my production environment is Linux-based.

It\'s possible that VirtualHost will be used. Assume that one filena

相关标签:
2条回答
  • 2020-12-07 15:58

    I recommend using File.join

    >> File.join("path", "to", "join")
    => "path/to/join"
    
    0 讨论(0)
  • 2020-12-07 15:58

    One thing to note. Ruby uses a "/" for file separator on all platforms, including Windows, so you don't actually need use different code for joining things together on different platforms. "C:/tmp/1.text" should work fine.

    File.join() is your friend for joining paths together.

    prefix_tmp_path = 'C:/tmp'
    filename = "#{rand(10)}.txt"
    fullname = File.join(prefix_tmp_path, filename) # e.g., C:/tmp/3.txt
    
    0 讨论(0)
提交回复
热议问题