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.
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.
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.
"dog".length => 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 the
countmethod on my string and pass it the argument "e."
"Welcome back, Elizabeth.".count("e") => 3
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:
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." sentence.upcase => "HELLO THERE."
The same is true for transforming a string to all lowercase.
sentence = "HELLO THERE." sentence.downcase => "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." sentence.downcase => "hello there." sentence => "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. Calling
downcaseonce changed the sentence to lowercase once, but it didn't update the original contents of my
Instead, I'll use downcase as a bang method:
sentence.downcase! sentence => "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.