Rubocop says use delete instead of sub
emyatsuna opened this issue · 3 comments
Hi,
I need to cleanup my pipeline rubocop syntax error. It is complaining with the replacement of empty of blank in my string. The .gsub
function works when tested but rubocop is not accepting it and my syntax in pipeline failed. I've checked and I can only use the .delete_prefix
and .delete_suffix
functions. Is there a way to use the delete funtcion as deleting all characters '/
' inside the mystr
?
sample string:
This /is a /test. Need to /replace or /delete the /slashes only in this /string.
C: Performance/StringReplacement: Use delete instead of gsub. (https://github.com/JuanitoFatas/fast-ruby#stringgsub-vs-stringtr-code)
str = mystr.gsub('\\', '')
Thank you.
Why not:
>> 'This /is a /test. Need to /replace or /delete the /slashes only in this /string.'.tr('/', '')
=> "This is a test. Need to replace or delete the slashes only in this string."
thanks @kotp ! That worked. My pipeline is not complaining about syntax errors via rubocop and the code still works.
You are welcome. Don't forget that you can always, even selectively, change Rubocop's opinion. See my answer on stack overflow to see some examples.