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, line 61
def byte_count
  respond_to?(:bytesize) ? bytesize : count_by_re(//n) - 1
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, line 70
def char_count
  count_by_re(/./um)
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, line 79
def count_by_re(re)
  scan(re).size
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, line 87
def count_by_re2(re)
  count = 0
  scan(re) { |_| count += 1 }
  count
end
line_count → anInteger click to toggle source

Count number of lines in str.

# File lib/nuggets/string/wc_mixin.rb, line 43
def line_count
  count_by_re(/#{$/}/)
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, line 35
def wc
  [wc_l, wc_w, wc_c]
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, line 52
def word_count
  count_by_re(/\S+/)
end
Also aliased as: wc_w