Skip to content

Commit ec660ca

Browse files
author
Bozhidar Batsov
committed
Add a rule suggesting the use of alternatives of String#gsub when possible
1 parent 043a580 commit ec660ca

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3037,6 +3037,23 @@ condition](#safe-assignment-in-condition).
30373037
end
30383038
```
30393039

3040+
* <a name="dont-abuse-gsub"></a>
3041+
Don't use `String#gsub` in scenarios in which you can use a faster more specialized alternative.
3042+
<sup>[[link](#dont-abuse-gsub)]</sup>
3043+
3044+
```Ruby
3045+
url = 'http://example.com'
3046+
str = 'lisp-case-rules'
3047+
3048+
# bad
3049+
url.gsub("http://", "https://")
3050+
str.gsub("-", "_")
3051+
3052+
# good
3053+
url.sub("http://", "https://")
3054+
str.tr("-", "_")
3055+
```
3056+
30403057
* <a name="heredocs"></a>
30413058
When using heredocs for multi-line strings keep in mind the fact that they
30423059
preserve leading whitespace. It's a good practice to employ some margin based

0 commit comments

Comments
 (0)