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(" ")
精彩评论