Understanding Ruby String Count Method Examples

nkemjiks

Mbonu Blessing

Posted on October 9, 2020

Understanding Ruby String Count Method Examples

Hello everyone, this week I am going to be explaining the examples that were written for the Ruby string count method. You all agree with me that understanding the first example is easy but applying that to the rest needed some extra digging.

Here is the link to the method https://apidock.com/ruby/String/count.

These are the example I will be explaining:



a = "hello world"
a.count "lo"                   #=> 5
a.count "lo", "o"              #=> 2
a.count "hello", "^l"          #=> 4
a.count "ej-m"                 #=> 4

"hello^world".count "\\^aeiou" #=> 4
"hello-world".count "a\\-eo"   #=> 4

c = "hello world\\r\\n"
c.count "\\"                   #=> 2
c.count "\\A"                  #=> 0
c.count "X-\\w"                #=> 3


Enter fullscreen mode Exit fullscreen mode

Our example string is hello world and we would be breaking down each of the example so you can understand it better.

a.count "lo"



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

#=> 5


Enter fullscreen mode Exit fullscreen mode

The above is pretty straight forward. It counts the number of times the characters passed appear in the string. Using the scan method for each of them.



"hello world".scan(/l/)
#=> ["l", "l", "l"] 

"hello world".scan(/o/)
#=> ["o", "o"]


Enter fullscreen mode Exit fullscreen mode

We see that 'l' appears 3 times and 'o' appears 2 times. this brings it to a total number of 5 times for the both of them.

a.count "lo", "o"



a = "hello world"
a.count "lo", "o"              

#=> 2


Enter fullscreen mode Exit fullscreen mode

What this does is it gets the intersection of the 2 parameters passed to it. From the above, our params are "lo" and "o". Intersecting the 2 of them. Let's try a simple code to find the intersection:



'lo'.split('') & 'o'.split('')
#=> ["o"]


Enter fullscreen mode Exit fullscreen mode

We can see that the intersection returns 'o' so our count will therefore return the number of times 'o' occurs in our example string. Hence the answer 2.

a.count "hello", "^l"



a = "hello world"
a.count "hello", "^l"          

#=> 4


Enter fullscreen mode Exit fullscreen mode

Let's take the 2 parameters apart. We know the 'hello' returns any character that matches 'h' or 'e' or 'l' or 'o' which gives us '7'.

You will notice the '^' in the above. It simple returns the number of characters that are not 'l' from. What this does is that from the we now return the number of character matches that is not 'l'. Which brings us to our final answer as '4'.

a.count "ej-m"



a = "hello world"
a.count "ej-m"                 

#=> 4


Enter fullscreen mode Exit fullscreen mode

Notice the '-' in the parameter passed. It simple defines a range between the character before it and the character after it. So it means return all characters that matches 'e' and alphabets between 'j' and 'm'. These alphabets are ['j', 'k', 'l', 'm']. From the range array, we can only find 'l' in our string so we are basically doing something like:



a.count "el"


Enter fullscreen mode Exit fullscreen mode

which returns 4.

"hello^world".count "\^aeiou"



"hello^world".count "\\^aeiou"

#=> 4


Enter fullscreen mode Exit fullscreen mode

Here, the backslashes negates the '^' characters and instead returns the characters that matches 'a' or 'e' or 'i' or 'o' or 'u' or '^'. Looking at the string, we see that only 3 vowels that matches the check and we have one '^' hence our answer 4.

"hello-world".count "a\-eo"



"hello-world".count "a\\-eo"   

#=> 4


Enter fullscreen mode Exit fullscreen mode

Same as the last example we just checked, the '\' negates the '-' and only finds characters that matches 'a' or 'e' or 'o' or '-'. We have 3 vowels and 1 '-' hence the answer 4 as well.

c.count "\"



c = "hello world\\r\\n"
c.count "\\" 

#=> 2


Enter fullscreen mode Exit fullscreen mode

This is quite straight forward. The first backlash is escaping the second one and the even the the string we are applying the count method to, the first backslash is escaping the second so the 2 backslashes are seen as one.

Based in this, we have 2 double backslashes hence our answer 2.

c.count "\A"



c = "hello world\\r\\n"
c.count "\\A"                 

#=> 0


Enter fullscreen mode Exit fullscreen mode

Just like the above, "\A" is looking for characters that matches exactly "\A" and we have zero. If we were looking for something like "\r", one will be returned. In a case where the double backslashes is followed by a random character in the string like d, the count for '\' is ignored and it returns just one.

Based on this, a search for "\A" returns 0.

c.count "X-\w"



c = "hello world\\r\\n"
c.count "X-\\w"                 

#=> 3


Enter fullscreen mode Exit fullscreen mode

We are going to break this down in 2 parts. The first part is "X-\". This is an ASCII character table I got form w3 schools.

ASCII character from X to '\'

Recall that the '-' signifies that we want to find a range between the character before and after. From the table, you can see that these characters are 'X', 'Y', 'Z', '[', and '\'. From our example string, only the '\' within the range is present there. And from 2 example before, we see that c.count("\") returns 2.

The last character is straight forward. We only have one occurrence of 'w' so combining the both together, we have our answer 3.

Resources

Stackoverflow - String#count options
Stackoveflow - How does count method works in Ruby?

This concludes the end of today's article. Feel free to share and comment your thought below.

Until next week

💖 💪 🙅 🚩
nkemjiks
Mbonu Blessing

Posted on October 9, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related