filename = filename.gsub("_"," ").nil? ? filename.gsub("_"," ") : filename
views:
61answers:
2
+5
A:
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 ?:.
sepp2k
2010-10-23 16:16:54
Ruby Documentation says: Performs the substitutions of String#gsub in place, returning str, or nil if no substitutions were performed
auralbee
2010-10-23 16:19:27
@auralbee: You're mixing up `gsub!` and `gsub`. You quoted the documentation for `gsub!`, but you used `gsub` in your code. `gsub` never returns `nil`, `gsub!` does.
sepp2k
2010-10-23 16:20:05
@sepp2k: You´re right! I was a little bit confused! Thanks
auralbee
2010-10-23 16:22:16
A:
basically, just
filename.gsub!("_", " ")
Or alternatively,
filename = filename.split("_").join(" ")
ghostdog74
2010-10-23 16:25:02