开发者

How to make this beautiful and short

开发者 https://www.devze.com 2023-01-21 21:02 出处:网络
filen开发者_StackOverflow中文版ame = filename.gsub(\"_\",\" \").nil? ? filename.gsub(\"_\",\" \") : filename
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

精彩评论

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