我正在尝试编写一个助手来将字符串从 转换"something_like_this"为"Something like this"。我习惯使用"something_like_this".titlecase它,"Something Like This"但除了第一个之外,我坚持将每个大写字母都小写。
我想我正在寻找这样的东西:
def write_sentence
string.titlecase.gsub!(/UPPERCASE-TO-LOWERCASE-EXCEPT-FIRST/)
#that should be something to lowercase everything except the first letter
return string
end
因此,在视图中,我可以编写string.write_sentence并让它返回我想要的内容。有什么想法吗?
谢谢!
编辑
我应该提一下,字符串有时可能只有一个单词,在这种情况下,字符串应该从 转换"something"为"Something".