加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Ruby:获取不带扩展名的文件名

发布时间:2020-12-16 19:28:46 所属栏目:百科 来源:网络整理
导读:如何获取没有扩展名的文件名?例如,“test.html.erb”应该是“测试” 类似question.但是, File.basename("test.html.erb",".*")= "test.html" 编辑:上述代码是我未能尝试产生所需结果的代码.我明白我正在使用File.basename.因此,我发布了这个问题.当我说文
如何获取没有扩展名的文件名?例如,“test.html.erb”应该是“测试”

类似question.但是,

> File.basename("test.html.erb",".*")
=> "test.html"

编辑:上述代码是我未能尝试产生所需结果的代码.我明白我正在使用File.basename.因此,我发布了这个问题.当我说文件名没有扩展名,我的意思是我不关心第一个点后的东西.我想要返回的名称不会有点.另外,在实际的代码中,我将传递__FILE__而不是“test.html.erb”.

解决方法

阅读文档:

basename(file_name [,suffix])→base_name

Returns the last component of the filename given in file_name,which
can be formed using both File::SEPARATOR and File::ALT_SEPARATOR as
the separator when File::ALT_SEPARATOR is not nil. If suffix is given
and present at the end of file_name,it is removed.

=> File.basename('public/500.html','.html')
=> "500"

在你的情况下

=> File.basename("test.html.erb",".html.erb")
=> "test"

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读