Module: Nuggets::String::WcMixin
- Included in:
- String
- Defined in:
- lib/nuggets/string/wc_mixin.rb
Instance Method Summary collapse
-
#byte_count ⇒ Object
(also: #wc_c)
call-seq: str.byte_count => anInteger.
-
#char_count ⇒ Object
(also: #wc_m)
call-seq: str.char_count => anInteger.
-
#count_by_re(re) ⇒ Object
call-seq: str.count_by_re(re) => anInteger.
-
#count_by_re2(re) ⇒ Object
call-seq: str.count_by_re2(re) => anInteger.
-
#line_count ⇒ Object
(also: #wc_l)
call-seq: str.line_count => anInteger.
-
#wc ⇒ Object
call-seq: str.wc => anArray.
-
#word_count ⇒ Object
(also: #wc_w)
call-seq: str.word_count => anInteger.
Instance Method Details
#byte_count ⇒ Object Also known as: wc_c
call-seq:
str.byte_count => anInteger
Count number of bytes in str.
61 62 63 |
# File 'lib/nuggets/string/wc_mixin.rb', line 61 def byte_count respond_to?(:bytesize) ? bytesize : count_by_re(//n) - 1 end |
#char_count ⇒ Object Also known as: wc_m
call-seq:
str.char_count => anInteger
Count number of characters in str.
70 71 72 |
# File 'lib/nuggets/string/wc_mixin.rb', line 70 def char_count count_by_re(/./um) end |
#count_by_re(re) ⇒ Object
call-seq:
str.count_by_re(re) => anInteger
Count number of occurrences of re
in str.
79 80 81 |
# File 'lib/nuggets/string/wc_mixin.rb', line 79 def count_by_re(re) scan(re).size end |
#count_by_re2(re) ⇒ Object
call-seq:
str.count_by_re2(re) => anInteger
A more memory-efficient version of #count_by_re.
87 88 89 90 91 |
# File 'lib/nuggets/string/wc_mixin.rb', line 87 def count_by_re2(re) count = 0 scan(re) { |_| count += 1 } count end |
#line_count ⇒ Object Also known as: wc_l
call-seq:
str.line_count => anInteger
Count number of lines in str.
43 44 45 |
# File 'lib/nuggets/string/wc_mixin.rb', line 43 def line_count count_by_re(/#{$/}/) end |
#wc ⇒ Object
call-seq:
str.wc => anArray
Count number of lines, words, and bytes in str.
35 36 37 |
# File 'lib/nuggets/string/wc_mixin.rb', line 35 def wc [wc_l, wc_w, wc_c] end |
#word_count ⇒ Object Also known as: wc_w
call-seq:
str.word_count => anInteger
Count number of words in str.
52 53 54 |
# File 'lib/nuggets/string/wc_mixin.rb', line 52 def word_count count_by_re(/\S+/) end |