fastruby / fast-ruby

:dash: Writing Fast Ruby :heart_eyes: -- Collect Common Ruby idioms.
https://github.com/fastruby/fast-ruby
5.67k stars 376 forks source link

Rubocop says use delete instead of sub #191

Closed emyatsuna closed 3 years ago

emyatsuna commented 3 years ago

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.

kotp commented 3 years ago

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."
emyatsuna commented 3 years ago

thanks @kotp ! That worked. My pipeline is not complaining about syntax errors via rubocop and the code still works.

kotp commented 3 years ago

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.