String#count options

This is one of the dorkiest ruby methods, and pretty lousy documentation to boot. Threw me for a loop. I ended up looking at it because it looked like it should give me the count of occurrences of a given string. Nope. Not remotely close. But here is how I ended up counting string occurrences:

s="this is a string with is thrice"
s.scan(/is/).count  # => 3

Makes me wonder why someone asked for this method, and why the documentation is so lousy. Almost like the person documenting the code really did not have a clue as to the human-understandable “business” reason for asking for this feature.

count([other_str]+) → fixnum

Each _other_str_ parameter defines a set of characters to count. The
intersection of these sets defines the characters to count in str. Any
_other_str_ that starts with a caret (^) is negated. The sequence c1–c2
means all characters between c1 and c2.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)