Understanding Ruby String Count Method Examples
Tags: ruby, beginners, webdev, rails
Published: 2020-10-09 13:50:09

---
title: Understanding Ruby String Count Method Examples
published: true
description: 
tags: #ruby #beginner #webdev #rails
//cover_image: https://direct_url_to_image.jpg
---

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:

```ruby
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
```

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"*
```ruby
a = "hello world"
a.count "lo"

#=> 5
```

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.

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

"hello world".scan(/o/)
#=> ["o", "o"]
```
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"*
```ruby
a = "hello world"
a.count "lo", "o"              

#=> 2
```

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:

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

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"*
```ruby
a = "hello world"
a.count "hello", "^l"          

#=> 4
```

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"*
```ruby
a = "hello world"
a.count "ej-m"                 

#=> 4
```
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:

```ruby
a.count "el"
```
which returns 4.

#### *"hello^world".count "\\^aeiou"*
```ruby
"hello^world".count "\\^aeiou"

#=> 4
```

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"*
```ruby
"hello-world".count "a\\-eo"   

#=> 4
```

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 "\\"*
```ruby
c = "hello world\\r\\n"
c.count "\\" 

#=> 2
```
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"*

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

#=> 0
```
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"*
```ruby
c = "hello world\\r\\n"
c.count "X-\\w"                 

#=> 3
```

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](https://www.w3schools.com/charsets/ref_html_ascii.asp#:~:text=ASCII%20is%20a%207%2Dbit,Z%2C%20and%20some%20special%20characters).

![ASCII character from X to '\'](https://dev-to-uploads.s3.amazonaws.com/i/e9cryqa70n2c3rr093i3.png)

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](https://stackoverflow.com/questions/5305638/stringcount-options)
[Stackoveflow - How does count method works in Ruby?](https://stackoverflow.com/questions/46514250/how-does-count-method-works-in-ruby/46516041#46516041)

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

Until next week