我有一些图像文件,我想在文件名的扩展名之前附加一个字符串。
示例:example.png
会变成 example-2x.png
所以,我想在扩展名之前插入 -2x
,可以是任何图像类型(png、jpg、gif 等)。
如果有一种简单的方法可以用 Ruby 做到这一点,那太好了。
最佳答案
Rake 有一个很好的 string extension用于操作路径:
require 'rake/pathmap'
"images/example.png".pathmap "%X-2x%x"
#=> "images/example-2x.png"
来自 pathmap
的文档:
- %X -- Everything but the file extension.
- %x -- The file extension of the path. An empty string if there is no extension.
关于ruby - 在文件名末尾插入字符(扩展名之前)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17094782/