Working With Ruby Strings, Arrays & Hashes

This is lesson #3 of the Complete Ruby Tutorial For Beginners series. You'll learn about different ways to work with & store data in Ruby.

What Is a String?

You’ve learned about variables & basic math operations in Ruby.

Next you’re going to learn about strings.

A string is a sequence of characters inside quotes.



You need the quotes so you can tell the difference between strings & variables.

A string is data.

A variable is a name for something.

In fact, you can use a variable to name a string:

food = "bacon"

This helps you reuse the string as many times as you want.

What Can You Do With Strings?

Strings can do many things for you.

For example, you can ask a string for its size:


# 5

Or you can tell the string to uppercase itself:



We call these “methods”, and there are many of them.

There are methods that allow you to substitute all or part of the string.

Like gsub:

"bacon".gsub("acon", "inary")

# "binary"

Or to split the string into a list of characters:


# ["b", "a", "c", "o", "n"]

Check this article for a more complete list of string methods.

When To Use Strings

You use strings whenever you want to print a message to the screen.

Like this:

puts "Hello There!"

Also when you read files, read input from the user (with the gets method), or when you want to combine several pieces of information together.

Strings can contain numbers, but that doesn’t mean you can treat them like numbers.

Here’s what I mean:

"1" + "1"

Gives you:


If you want to convert a string with numbers into an actual integer value you have to use the to_i method.

Remember: A method is a command you can give to any object in Ruby. Depending on the kind of object you’re working with they will respond to different methods.

Converting a string to integer:


Converting an integer to a string:


Why is there a difference?

Because integers are numbers, they must behave like numbers & allow for mathematical operations.

Strings have a different purpose, and a different set of methods.

How to Combine Strings Using String Interpolation

To combine numbers & strings you need a technique named “string interpolation”.

Here’s an example:

age = 20
name = "David"

puts "Hello #{name}, our records tell us that you're #{age} years old!"

This is like a template.

Ruby replaces these #{name} & #{age} by their values, producing the combined string.

How to Use Arrays

If you want to have many of the same thing then arrays are very useful.

An array is a collection of items in a single location.

Here’s an example:

[1, 2, 3, 4, 5]

This is an array of integers.

You can access every element by its position.

We call that position an index.


letters = ['a', 'b', 'c']

# 'a'

# 'b'

# 'c'


The first element in an array is always on position 0.

Don’t ask me why, but you have to remember that.

If you ask for an index that is bigger than the array size you’ll get a nil value.

It looks like this:


# nil

A nil value is Ruby telling you:

“I can’t find what you want so here is a generic response”

And just like strings, arrays have a set of methods you can use to make them do things.

For example:


# 3

You can add new elements to an array like this:

numbers = []

numbers << 1
numbers << 2
numbers << 3

# [1, 2, 3]

This is a very useful array method, so write it down.

Both strings & arrays are very important building blocks for writing your Ruby programs.

If you don’t understand how these 2 work you won’t be able to write even the most basic projects you can think of.

Don’t just read this…

Go open irb now & practice.

If you don’t know what irb is or how to open it you need to go back to chapter 1 of this guide.

How to Use a Ruby Hash

A hash is like a dictionary.

It helps you match two values together

…like a domain name to an IP address, or a phone number to a person’s name.

Here’s how to create a hash in Ruby:

ip_to_domain = { "" => "" }

You can get the value for a hash key like this:


# ""

And you can change the value like this:

ip_to_domain[""] = ""

These are the 3 main operations you can do with a hash.

Btw, you use a comma to separate multiple key/value pairs.

Like this:

  values = { "a" => 1, "b" => 2 }

You can find a list of hash methods here.

Do These Exercises Now

  • Create an array with the name of food with these values: “bacon”, “orange”, “apple”
  • Access the first element of the food array
  • Access the last elements of the food array
  • Add a new element into the array: “yogurt”
  • Create a hash with 3 key/value pairs representing country codes (like ES) & their country names (like Spain).
  • Answer this question: What is the difference between 1 and "1"?