Working with strings in Ruby

Mar 25, 2015

Table of contents:

  1. What is a string?
  2. Concatenation
  3. Case
  4. Length
  5. Strip
  6. Conclusion

Last week we began our adventure into the wonderful world of Ruby by looking at some of the reasons why you would want to invest your time into learning it.

We also set up our computers to run Ruby and we started to play around with what we can do with numbers.

Strings are one of the foundational data types of any programming language.

In today’s tutorial we continue to use IRB to understand how we can use and manipulate strings in Ruby.

If you missed last week’s tutorial, you might want to read that before continuing with today’s tutorial.

What is a string?

Before we get into playing with strings in IRB, first it’s important to understand what a string actually is.

A string in computer programming is one of the primitive data types of a programming language. Strings are typically used as literal constant or as some kind of variable.

For example a string might be used to store the user’s name:

name = 'Jane'

In Ruby, a string is an object that holds a sequence of characters. You can create a new string by creating a new object, or simple creating a string literal.

The String object has a number of useful methods that can be invoked in order to manipulate the string.

In today’s tutorial we will be exploring the methods of the String object.

Fire up IRB by going into your terminal and typing:


Next create a new string:

name = 'Jane'

name is now an instance of the String ruby object. We can check to make sure this is the case by calling the class method:

# => String

In the example above we create a new String object as a literal. However we can also instantiate a String using the new method:

person ='Bob')
# => "Bob"

# => String

Now that we know how to create new instances of the String object, we can start to explore some of the instance methods of the class.


One of the most basic things you will find yourself doing with strings is concatenation. This is where you join two strings together. There are a few different ways to do this in Ruby.

Firstly you can join two strings together using the + operator:

greeting = 'hello ' + 'world'
# => "hello world"

If you already have a string you can append another string using the << operator:

name = 'Philip '
# => "Philip "

name << 'Brown'
# => "Philip Brown"

You can also multiple a string by an integer to return three copies of that string as a new string:

'yo' * 3
# => "yoyoyo"


We can convert a string into capital case by calling the capitalize method:

# => "Philip"

When you call a method on a string, such as capitalize, you will be returned a new string.

For example, try the following in IRB:

name = 'philip'
# => "philip"

# => "Philip"

Now if you puts the name variable, what do you think it will be?

puts name
# => "philip"

When you call a method on a String object it creates a new string, rather than changing the existing one.

To modify the original instance we can call the same method but with a !:

# => "Philip"

# => "Philip"

If you want to convert a string to lowercase you can call the downcase method:

'HELLO WORLD'.downcase
# => "hello world"

Alternatively you can covert a string to uppercase using the upcase method:

'hello world'.upcase


If you need to count the number of characters in a string you can call use the length method:

# => 9


Whenever you accept input from a user you should make sure they haven’t included any whitespace. You can remove white space using the strip method:

'philip brown '.strip
# => "philip brown"

If you want to modify the current instance of the string rather than create a new string, you should call the method with a !:

name = ' philip brown '
# => " philip brown "

# => "philip brown"

# => "philip brown"

Alternatively you can only remove whitespace from the left or the right of the string by calling lstrip or rstrip.


There are many more useful String methods available that make working with strings in Ruby a breeze, however there isn’t much point in memorising all of these methods when the Ruby documentation is only a Google search away.

Firstly, the two important things to take away from this tutorial is, when you call a method on a String instance you will be returned a new object.

Secondly, if you want to modify the original object, you should call the method with a !.

I think rather than learning the syntax to every method of every object, a much better way of learning a new language is to understand the rules around how the language works.

When you can think in terms of the language you will have a much deeper understanding and you will know the right questions to ask when you get stuck on a particular problem.

Philip Brown


© Yellow Flag Ltd 2024.