module Nuggets::String::WcMixin

Public Instance Methods

byte_count → anInteger click to toggle source

Count number of bytes in str.

   # File lib/nuggets/string/wc_mixin.rb
61 def byte_count
62   respond_to?(:bytesize) ? bytesize : count_by_re(//n) - 1
63 end
Also aliased as: wc_c
char_count → anInteger click to toggle source

Count number of characters in str.

   # File lib/nuggets/string/wc_mixin.rb
70 def char_count
71   count_by_re(/./um)
72 end
Also aliased as: wc_m
count_by_re(re) → anInteger click to toggle source

Count number of occurrences of re in str.

   # File lib/nuggets/string/wc_mixin.rb
79 def count_by_re(re)
80   scan(re).size
81 end
count_by_re2(re) → anInteger click to toggle source

A more memory-efficient version of count_by_re.

   # File lib/nuggets/string/wc_mixin.rb
87 def count_by_re2(re)
88   count = 0
89   scan(re) { |_| count += 1 }
90   count
91 end
line_count → anInteger click to toggle source

Count number of lines in str.

   # File lib/nuggets/string/wc_mixin.rb
43 def line_count
44   count_by_re(/#{$/}/)
45 end
Also aliased as: wc_l
wc → anArray click to toggle source

Count number of lines, words, and bytes in str.

   # File lib/nuggets/string/wc_mixin.rb
35 def wc
36   [wc_l, wc_w, wc_c]
37 end
wc_c()
Alias for: byte_count
wc_l()
Alias for: line_count
wc_m()
Alias for: char_count
wc_w()
Alias for: word_count
word_count → anInteger click to toggle source

Count number of words in str.

   # File lib/nuggets/string/wc_mixin.rb
52 def word_count
53   count_by_re(/\S+/)
54 end
Also aliased as: wc_w