Ruby Capitalization Method

Hi, everybody!

I just have a wonder…

Is there a way in Ruby that can help us figure out capitalizing every first letter in each independent sentence if at some point we wanted the user to type a paragraph so that we will be sure that the program will take care of it in case the user messed something up?

I mean, is there some method like capitalize but capitalizes every letter at the beginning of each new sentence not just the very first one? If yes, how could Ruby know when the sentence begins and when it ends?

I am thinking of writing a snippet manually to do it, like:
“Hey, Ruby! If you see any (., ! or ?), please capitalize the following letter.”

This idea came to me just while writing this inquiry, but don’t know clearly how I am gonna do it.
I’m gonna give it a shot!

You are on the right track. It’s hard to fully pinpoint the rules of a human language. Look into the split method, and regular expressions those will help you.


Thanks! I am going to find out.

Are you just wanting to be sure each sentence starts with a capital letter?

1 Like

Yes, sir.

Thank you! That link was useful.

But it talks about capitalizing the 1st letter of every single word, not just the 1st word of every sentence I think?

1 Like

That was my question, to clarify what it is you expect. So we now know for certain you want to have proper sentence capitalization. We can go from there. What have you come up with, so far?

As suggested earlier by @fight_dragons,

but we need not go into regex at this point if you are just learning. Let’s look for another way that Ruby will help us.

Ruby lets us split on any character. We know that there are only four full-stop characters in english grammar.

. ? ! : 

Notice they all have a period (dot) on the baseline.

We could add in the horizontal ellipsis for good measure, but that escalates the problem to a new level we don’t need to explore, just now.

['.','?','!',':'].each { |x| text.split(x) }

That’s largely incomplete, but should give you some ideas.

1 Like

Actually I did not write any code for it yet, I was trying to do some research and got those articles to study:

Yeah, I feel it’s more obvious now. I will proceed more with that as I move forward through Ruby’s CodeCademy curriculum. Thanks for your time, sir!

1 Like