tags:

views:

61

answers:

2
filename = filename.gsub("_"," ").nil? ? filename.gsub("_"," ") : filename
+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
Ruby Documentation says: Performs the substitutions of String#gsub in place, returning str, or nil if no substitutions were performed
auralbee
@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
@sepp2k: You´re right! I was a little bit confused! Thanks
auralbee
A: 

basically, just

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

Or alternatively,

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