String#count options

前端 未结 6 767
Happy的楠姐
Happy的楠姐 2021-02-02 06:01

From the documentation for String#count I understand the first example, but I do not understand the rest of the examples:

a = \"hello world\"
a.count \"lo\"              


        
6条回答
  •  感动是毒
    2021-02-02 06:25

    If you pass more than 1 parameter to count, it will use the intersection of those strings and will use that as the search target:

    a = "hello world"
    a.count "lo"            #=> finds 5 instances of either "l" or "o"
    a.count "lo", "o"       #=> the intersection of "lo" and "o" is "o", so it finds 2 instances
    a.count "hello", "^l"   #=> the intersection of "hello" and "everything that is not "l" finds 4 instances of either "h", "e" or "o"
    a.count "ej-m"          #=> finds 4 instances of "e", "j", "k", "l" or "m" (the "j-m" part)
    

提交回复
热议问题