开发者

How to make this beautiful and short

filen开发者_StackOverflow中文版ame = filename.gsub("_"," ").nil? ? filename.gsub("_"," ") : filename


filename = filename.gsub("_", " ")

Or if it's ok to mutate the string:

filename.gsub!("_", " ")

Checking whether gsub returns nil is completely unnecessary - gsub never returns nil. gsub! returns nil if no changes have been made, but if you use gsub!, you usually don't care about the return value anyway.

Also note that the code you gave will always return filename unchanged because you mixed up the then- and the else-part of your ?:.


basically, just

filename.gsub!("_", " ")

Or alternatively,

filename = filename.split("_").join(" ")
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜