From 6d7a922da4e21bbe28e9a13a44a98d29e18a1aac Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Sat, 20 Mar 2021 01:20:44 +0900 Subject: [PATCH] Add new "String#chars" rule Follow https://github.com/rubocop/rubocop/pull/9615. This PR adds "String#chars" rule. ```ruby # bad string.split(//) string.split('') # good string.chars ``` --- README.adoc | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/README.adoc b/README.adoc index 597bd8d8..5c695d28 100644 --- a/README.adoc +++ b/README.adoc @@ -4524,6 +4524,22 @@ url.sub('http://', 'https://') str.tr('-', '_') ---- +=== `String#chars` [[string-chars]] + +Prefer the use of `String#chars` over `String#split` with empty string or regexp literal argument. + +NOTE: These cases have the same behavior since Ruby 2.0. + +[source,ruby] +---- +# bad +string.split(//) +string.split('') + +# good +string.chars +---- + === `sprintf` [[sprintf]] Prefer the use of `sprintf` and its alias `format` over the fairly cryptic `String#%` method.