Alternative To The .count Method In The Ruby Programming Language

As someone who is learning how to code, we all have to go through the usual coding challenges and exercises. One of many is finding a way to solve a challenge without using the obvious route. Today I wanted to tackle this and maybe provide some relief and aid towards all the coding students out there having to figure out alternative solutions to those problems.

Today I will tackle an alternative to the .count method.

If you are interested in me finding more alternatives then feel free to comment and I can put out an article for that.

Let’s get started!

For the purpose of teaching, I will make up a prompt.
“Define a method that accepts a two strings. One string will be a word or sentence and the second will be a character or set of characters. We are looking for how many times the second string appears in the first.”

Before we start, feel free to try this on your own. If you found another way to complete this problem the leave it in the comments so others can see how you did it. I’m also using Visual Studio Code to solve this. If you use another text editor then thats absolutely fine. Remember to follow along. It helps build muscle memory.

Let’s get to it.

  1. The way I like to solve these problems is by creating my own examples and using the .count method just so I have the correct answers to check my work against. This is totally optional but for me it helps me know I am on the right path when working these types of coding exercises out.

Use any example you would like. For me, these are my example that I will be checking my work against.

p ("I'M THAT GIRL").count("I")

=> 2

p ("ALIEN SUPERSTAR").count("SR")

=> 4

p ("PURE/HONEY").count("E")

=> 2

p ("ALL UP IN YOUR MIND").count("LI")

=> 4

p ("VIRGOS GROOVE").count("O")

=> 3

p ("SUMMER RENAISSANCE").count("MISE")

=> 9

Now that we know what the correct answers are. Let’s start with the actual solution.

Prompt: “Define a method that accepts a two strings. One string will be a world or sentence and the second will be a character or set of characters. We are looking for how many times the second string appears in the first.”

  1. I usually start out with a basic format so from the first sentence I know I will need a method with two parameters. The method will be used to check how many times certain characters appear in a string. Simple enough.
def character_count(param1, param2)

end

You can rename the parameters. I just used those as place holders.

The way I will approach this is that I know i will need some sort of count in the method. Not the .count method but an increment count throughout each letter of the string. I also think I may need an if statement so lets see.

The first thing I will focus on it the count.

def character_count(string, letter)
count = 0

end

Somewhere throughout this I will need to increment the count so that each character in the string is being checked. I also know I will need a do/end block that contains an if statement. If the string includes a character then the count should increment to check all of it.

Before I get into the code, I just wanna say that Ruby is a direct programming language. Often time you just need to think about what you will do and most likely you will get the answers just by thinking about the process. In the previous paragraph, my basic though process practically gave the answer away. All I need to do is work out a few things and we will have a functioning code. Let me show you what I mean.

If I type in everything I stated in the paragraph, the code will look like this:

def character_count(string, letter)
count = 0
count +1
each_chars
do /end
if then


end

This is OBVIOUSLY not a functioning piece of code but this is to show that just by thinking about the process itself, you can get the answer you are looking for. Just remember to keep it DRY and it will help you get there faster and more efficiently.

So what will this code look like if I type it in a way that Ruby will execute it

def character_count(string, letter)
count = 0
string.each_chars do |character|
if letter.include?(character)
then count+=1
end
end
end

This is not as DRY as I would like it and it also doesn’t give us the answer we are quite looking for. Let’s make a few adjustments before we run it.

def character_count(string, letter)
count = 0
string.each_char { |character| count +=1 if letter.include?(character)}
count

end

puts character_count("I'M THAT GIRL", "I")

=> 2

puts character_count("ALIEN SUPERSTAR", "SR")

=> 4

puts character_count("PURE/HONEY", "E")

=> 2

puts character_count("ALL UP IN YOUR MIND", "LI")

=> 4

puts character_count("VIRGOS GROOVE", "O")

=> 3

puts character_count("SUMMER RENAISSANCE", "MISE")

=> 9

So let’s break down what exactly I did. First I put the count at 0 as we will be incrementing it and it needs a value to start at. Secondly, I did an each method on the first parameter (string) with a one line do/end block. The each method of .each_chars to be exact and it is exactly what it sounds like. EACH CHARACTER.

The do/end block is incrementing the count throughout the string to check IF the letters are INCLUDED within string. Exactly like the code reads. We then put count on the third line to get back the output which we see.

This wasn’t the most simple alternative but I hope it provided some relief towards those looking for it. A few lines of code and we got exactly what we are looking for. Feel free to change the string and pass in anything you want to test this on.

If you are interested in some more articles like this then please thumbs up and follow. Thank you and I wish you well on your coding journey!

--

--