• 20 hours
  • Easy

Free online content available in this course.



Got it!

Last updated on 2/6/20

Run methods on strings

Log in or subscribe for free to enjoy all this course has to offer!

Strings in Ruby come equipped with some predefined methods that give you access to information about the string itself and allow you to perform operations on the string.

In this chapter, we'll see how to manipulate text within a string to make it all uppercase, all lowercase, calculate the number of characters in a string, and more. 

Measuring strings

You can measure strings numerically in a few different ways in Ruby. It's practical to be able to evaluate both the entire length of a string as well as the number of times a certain character appears in it.

String length

Being able to calculate the number of characters in a string is useful. For example, you could have a rule on your website that all users must have passwords longer than 20 characters. If someone tries to set their password as "abc," you'd want them to receive an error message. 

To calculate the number of characters in a string, you can call the length method on it. 

=> 3

"This is a very, very, very, very, very, very, long sentence.".length
=> 60

This will work on any string, large or small. Once you become more advanced in Ruby development, you can make certain things happen in your code depending on the length of a string.

String character count of a particular type

Instead of counting the total number of characters in a string, you can also count the number of times one particular character appears in the string. 

To check the number of times the character "e" appears in a string, I simply call thecountmethod on my string and pass it the argument "e." 

"Welcome back, Elizabeth.".count("e")
=> 3

While these sorts of calculations might not seem obvious in terms of usefulness, the more control you have over the text in your web projects, the better. You'd be surprised at how important it is for your code to understand every detail of the objects you're working with. 

Modifying strings

You can also modify strings in Ruby in order to exert greater control over the way strings might display on your website or be stored in a database. 

Notice on OpenClassrooms that we capitalize the names of projects and courses on the display of a path page: 

Capitalizing strings using code
Capitalizing strings using code

The project and course names aren't stored in our database in all uppercase like this. The transformation of the titles to uppercase (for display purposes) happens within our code itself. OpenClassrooms isn't written in Ruby, but you can do this same thing in Ruby as well as other programming languages!

To transform a string into all uppercase in Ruby, simply call the upcase method on the string. 

sentence = "Hello there."

The same is true for transforming a string to all lowercase.

sentence = "HELLO THERE."
=> "hello there"

However, running the upcase or downcase methods (or other string modification methods in Ruby) doesn't actually change the original string content itself. Huh?

sentence = "HELLO THERE."
=> "hello there."

In order to modify the original contents of the string, instead of just modifying the string once when you first run the method, you'll use a bang method.

Bang methods in Ruby

If you find that the Ruby method you're calling isn't actually updating the object you want in a permanent way, simply add an exclamation point to the end of the method. 

To repeat, in the example above, I took a sentence variable that was written in all capital letters. Callingdowncaseonce changed the sentence to lowercase once, but it didn't update the original contents of mysentencevariable. 

Instead, I'll use downcase as a bang method: 

=> "hello there."

Adding an exclamation point to the end of the method name indicates to Ruby, "Hey, I don't want you to just modify this string once. I want you to change the contents of the string itself permanently."

There are many ways to use bang methods in Ruby, but using them on strings allows you to finesse the format of text in your Ruby code.

Example of certificate of achievement
Example of certificate of achievement