Skip to content

Commit b0db93c

Browse files
[DOC] Tweaks for String#count
1 parent 9e7a985 commit b0db93c

File tree

2 files changed

+79
-17
lines changed

2 files changed

+79
-17
lines changed

doc/string/count.rdoc

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
Returns the total number of characters in +self+ that are specified by the given selectors.
2+
3+
For one 1-character selector,
4+
returns the count of instances of that character:
5+
6+
s = 'abracadabra'
7+
s.count('a') # => 5
8+
s.count('b') # => 2
9+
s.count('x') # => 0
10+
s.count('') # => 0
11+
12+
s = 'тест'
13+
s.count('т') # => 2
14+
s.count('е') # => 1
15+
16+
s = 'よろしくお願いします'
17+
s.count('よ') # => 1
18+
s.count('し') # => 2
19+
20+
For one multi-character selector,
21+
returns the count of instances for all specified characters:
22+
23+
s = 'abracadabra'
24+
s.count('ab') # => 7
25+
s.count('abc') # => 8
26+
s.count('abcd') # => 9
27+
s.count('abcdr') # => 11
28+
s.count('abcdrx') # => 11
29+
30+
Order and repetition do not matter:
31+
32+
s.count('ba') == s.count('ab') # => true
33+
s.count('baab') == s.count('ab') # => true
34+
35+
For multiple selectors,
36+
forms a single selector that is the intersection of characters in all selectors
37+
and returns the count of instances for that selector:
38+
39+
s = 'abcdefg'
40+
s.count('abcde', 'dcbfg') == s.count('bcd') # => true
41+
s.count('abc', 'def') == s.count('') # => true
42+
43+
In a character selector, three characters get special treatment:
44+
45+
- A caret (<tt>'^'</tt>) functions as a _negation_ operator
46+
for the immediately following characters:
47+
48+
s = 'abracadabra'
49+
s.count('^bc') # => 8 # Count of all except 'b' and 'c'.
50+
51+
- A hyphen (<tt>'-'</tt>) between two other characters defines a _range_ of characters:
52+
53+
s = 'abracadabra'
54+
s.count('a-c') # => 8 # Count of all 'a', 'b', and 'c'.
55+
56+
- A backslash (<tt>'\'</tt>) acts as an escape for a caret, a hyphen,
57+
or another backslash:
58+
59+
s = 'abracadabra'
60+
s.count('\^bc') # => 3 # Count of '^', 'b', and 'c'.
61+
s.count('a\-c') # => 6 # Count of 'a', '-', and 'c'.
62+
'foo\bar\baz'.count('\\') # => 2 # Count of '\'.
63+
64+
These usages may be mixed:
65+
66+
s = 'abracadabra'
67+
s.count('a-cq-t') # => 10 # Multiple ranges.
68+
s.count('ac-d') # => 7 # Range mixed with plain characters.
69+
s.count('^a-c') # => 3 # Range mixed with negation.
70+
71+
For multiple selectors, all forms may be used, including negations, ranges, and escapes.
72+
73+
s = 'abracadabra'
74+
s.count('^abc', '^def') == s.count('^abcdef') # => true
75+
s.count('a-e', 'c-g') == s.count('cde') # => true
76+
s.count('^abc', 'c-g') == s.count('defg') # => true
77+
78+
Related: see {Querying}[rdoc-ref:String@Querying].

string.c

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9111,23 +9111,7 @@ rb_str_tr_s(VALUE str, VALUE src, VALUE repl)
91119111
* call-seq:
91129112
* count(*selectors) -> integer
91139113
*
9114-
* Returns the total number of characters in +self+
9115-
* that are specified by the given +selectors+
9116-
* (see {Multiple Character Selectors}[rdoc-ref:character_selectors.rdoc@Multiple+Character+Selectors]):
9117-
*
9118-
* a = "hello world"
9119-
* a.count "lo" #=> 5
9120-
* a.count "lo", "o" #=> 2
9121-
* a.count "hello", "^l" #=> 4
9122-
* a.count "ej-m" #=> 4
9123-
*
9124-
* "hello^world".count "\\^aeiou" #=> 4
9125-
* "hello-world".count "a\\-eo" #=> 4
9126-
*
9127-
* c = "hello world\\r\\n"
9128-
* c.count "\\" #=> 2
9129-
* c.count "\\A" #=> 0
9130-
* c.count "X-\\w" #=> 3
9114+
* :include: doc/string/count.rdoc
91319115
*/
91329116

91339117
static VALUE

0 commit comments

Comments
 (0)