How to get filename without extension from file path in Ruby

前端 未结 9 1221
感情败类
感情败类 2020-11-29 17:04

How can I get the filename from a file path in Ruby?

For example if I have a path of \"C:\\projects\\blah.dll\" and I just want the \"blah\".

Is

9条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-29 17:23

    In case the extension is not known (it needs the / separator):

    irb(main):024:0> f = 'C:\foobar\blah.txt'.gsub("\\","/")
    => "C:/foobar/blah.txt"
    irb(main):027:0> File.basename(f,File.extname(f))
    => "blah"
    

提交回复
热议问题