An Advanced Way to Combine Strings Together in Ruby - dummies

An Advanced Way to Combine Strings Together in Ruby

By Christopher Haupt

Ruby has a type of data called an array that you can use to combine strings together. Think of an array like a special storage box with multiple compartments. You can put a different object in each compartment and separately retrieve those objects.

After numbers and strings, arrays are perhaps one of the most common data types you’ll encounter when programming.

Ruby represents arrays using square brackets like this:

2.2.2 :013 > big_a_array = [a1, a2, a3, a4]
=> ["   A   ", "  A A  ", " AAAAA ", "A     A"]

In this example, you assign the array to a new variable called big_a_array and put the separate a1, a2, a3, and a4 variables into the array.

The cool thing is that if you print out the array with puts, Ruby does the right thing automatically:

2.2.2 :014 > puts big_a_array
   A
  A A
 AAAAA
A     A
=> nil

This technique saves a lot of typing!