Write your implementation of Ruby's String#count method The task is to write a simplified version of Ruby's String#count method oneself. Simplified because it doesn't have to have the negation and sequence parts of the original.
Here my solution:
def custom_count(string, search_char)
    sum = 0
    seg_string = string.split(&quot;&quot;)
    seg_char = search_char.split(&quot;&quot;)

    for i in seg_string
        for j in seg_char
            if i == j
                sum += 1
            end
        end
    end

    sum
end

p custom_count(&quot;Hello, World!&quot;, &quot;l&quot;)    # =&gt; 3
p custom_count(&quot;Hello, World!&quot;, &quot;lo&quot;)   # =&gt; 5
p custom_count(&quot;Hello, World!&quot;, &quot;le&quot;)   # =&gt; 4
p custom_count(&quot;Hello, World!&quot;, &quot;x&quot;)    # =&gt; 0
p custom_count(&quot;Hello, World!&quot;, &quot;w&quot;)    # =&gt; 0
p custom_count(&quot;Hello, World!&quot;, &quot;W&quot;)    # =&gt; 1

Is there a better way to solve the task? Perhaps without using nested loops?
If you can use some ruby String method I would suggest
def custom_count(string, search_char)
  search_char.split(&quot;&quot;).inject(0) do |count,pattern|
    string.scan(pattern).size + count
  end
end