[#111712] [Ruby master Feature#19322] Support spawning "private" child processes — "kjtsanaktsidis (KJ Tsanaktsidis) via ruby-core" <ruby-core@...>
SXNzdWUgIzE5MzIyIGhhcyBiZWVuIHJlcG9ydGVkIGJ5IGtqdHNhbmFrdHNpZGlzIChLSiBUc2Fu
14 messages
2023/01/07
[ruby-core:111903] [Ruby master Feature#13890] Allow a regexp as an argument to 'count', to count more interesting things than single characters
From:
"sawa (Tsuyoshi Sawada) via ruby-core" <ruby-core@...>
Date:
2023-01-19 08:59:21 UTC
List:
ruby-core #111903
Issue #13890 has been updated by sawa (Tsuyoshi Sawada).
Can't overlapping be realized by putting the overlapping part within a look=
-ahead? Is there a use case where it cannot?
----------------------------------------
Feature #13890: Allow a regexp as an argument to 'count', to count more int=
eresting things than single characters
https://bugs.ruby-lang.org/issues/13890#change-101324
* Author: duerst (Martin D=FCrst)
* Status: Open
* Priority: Normal
----------------------------------------
Currently, String#count only accepts strings, and counts all the characters=
in the string.
However, I have repeatedly met the situation where I wanted to count more i=
nteresting things in strings.
These 'interesting things' can easily be expressed with regular expressions.
Here is a quick-and-dirty Ruby-level implementation:
````Ruby
class String
alias old_count count
def count (what)
case what
when String
old_count what
when Regexp
pos =3D -1
count =3D 0
count +=3D 1 while pos =3D index(what, pos+1)
count
end
end
end
````
Please note that the implementation counts overlapping occurrences; maybe t=
here is room for an option like `overlap: :no`.
--=20
https://bugs.ruby-lang.org/
______________________________________________
ruby-core mailing list -- ruby-core@ml.ruby-lang.org
To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
ruby-core info -- https://ml.ruby-lang.org/mailman3/postorius/lists/ruby-c=
ore.ml.ruby-lang.org/