gpt4 book ai didi

Ruby:获取没有扩展名的文件名

转载 作者:数据小太阳 更新时间:2023-10-29 06:26:59 26 4
gpt4 key购买 nike

如何获取没有扩展名的文件名?例如,输入 "/dir1/dir2/test.html.erb" 应该返回 "test"

在实际代码中,我将传递 __FILE__ 而不是 "/dir1/dir2/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"

关于Ruby:获取没有扩展名的文件名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23356777/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com