Busy learning Ruby... the documentation have an example:
"hello world".count("lo", "o") that return 2 how does that return 2?
In my example I've: puts "Lennie".count("Le", "ie") that return 2.
How does count work in this regard?
"hello world".count("lo")
returns five. It has matched the third, fourth, fifth, eighth, and tenth characters. Lets call this set one.
"hello world".count("o")
returns two. It has matched the fifth and eighth characters. Lets call this set two.
"hello world".count("lo", "o")
counts the intersection of sets one and two.
The intersection is a third set containing all of the elements of set two that are also in set one. In our example, both sets one and two contain the fifth and eighth characters from the string. That's two characters total. So, count
returns two.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With