How to Create a Large HELLO Message in Ruby

By Christopher Haupt

Here’s how to create some super-sized letters to print out a large “Hello” message in Ruby. You will be building each letter from a combination of strings that, when printed out, form the shapes of a large letter.

Creating the letter H

Start by creating the string parts for the big letter H:

  1. Create the first h1 variable. This time, use nine characters for the total size of the string. In this step, there will be seven spaces between the two H’s:

    2.2.2 :015 > h1 = "H       H"
    => "H       H"
  2. Create the h2 and h3 variables, which are identical to Step 1:

    2.2.2 :017 > h2 = "H       H"
    => "H       H"
    2.2.2 :018 > h3 = "H H"
    => "H       H"
  3. Check that any of your variables are the right size by using the string object’s length method to print out its number of characters:

    2.2.2 :019 > h3.length
    => 9
  4. Create the h4 variable, which is the middle of the letter H:

    2.2.2 :020 > h4 = "HHHHHHHHH"
    => "HHHHHHHHH"

    Did you notice that you repeated yourself a lot for h2 and h3? The letter H is interesting because the top and bottom parts of the letter (at least for the capital version we’re using) is the same.

    Programmers say that the two parts of the letter are symmetric. You can use the fact that the top and bottom are the same to save some work.

  5. Create h5 by assigning it the value of h1, because they look the same:

    2.2.2 :021 > h5 = h1
    => "H       H"
  6. Repeat Step 5 for variables h6 and h7:

    2.2.2 :022 > h6 = h1
    => "H       H"
    2.2.2 :023 > h7 = h1
    => "H       H"
  7. Put all the parts of the letter into an array for storage and test it out. Use the variable named h to hold the array:

2.2.2 :024 > h = [h1,h2,h3,h4,h5,h6,h7]
=> ["H       H", "H       H", "H       H", "HHHHHHHHH", "H       H", "H       H", "H       H"]
2.2.2 :025 > puts h
H       H
H       H
H       H
HHHHHHHHH
H       H
H       H
H       H
=> nil

Creating the letter E

Next up is the letter E. You’ll use the same general techniques that you just used for the letter H.

  1. Create the first e1 variable. Use nine E characters for the total size of the string:

    2.2.2 :026 > e1 = "EEEEEEEEE"
    => "EEEEEEEEE"
  2. Create the next variable, e2. This one is a little tricky, because for the vertical part of the letter E, you need to make sure that you account for both the visible part of the letter and the whitespace:

    2.2.2 :027 > e2 = "E        "
    => "E        "
  3. The letter E is pretty repetitive and uses one or the other of the two parts you’ve already created. Using the timesaving technique you learned for the previous letter, make the e3 variable the same as e2:

    2.2.2 :028 > e3 = e2
    => "E        "
  4. The fourth variable, e4, will store the middle horizontal part of the letter. For this project, make it the same as the top part:

    2.2.2 :029 > e4 = e1
    => "EEEEEEEEE"
  5. Time for some more whitespace, so make the next two variables store the same value as e2:

    2.2.2 :030 > e5 = e2
    => "E        "
    2.2.2 :031 > e6 = e2
    => "E        "
  6. Now, create e7 to hold the bottom of the letter:

    2.2.2 :032 > e7 = e1
    => "EEEEEEEEE"
  7. Store the separate variables in an array and assign that to the variable e. Test it to make sure that it looks right:

2.2.2 :034 >   e = [e1,e2,e3,e4,e5,e6,e7]
=> ["EEEEEEEEE", "E        ", "E        ", "EEEEEEEEE", "E        ", "E        ", "EEEEEEEEE"]
2.2.2 :035 > puts e
EEEEEEEEE
E
E
EEEEEEEEE
E
E
EEEEEEEEE
=> nil

Creating the letter L

The letter L is even easier, because it’s really only made of two unique parts.

  1. Create the first variable l1 (that’s the lowercase letter L and the numeral for one):

    2.2.2 :036 > l1 = "L        "
    => "L        "
  2. Almost all of the letter L is made up of the same pattern as what you stored in l1, so you’ll reuse that variable when you store it in an array. Instead, skip ahead to the seventh piece of the shape and create variable l7:

    2.2.2 :037 > l7 = "LLLLLLLLL"
    => "LLLLLLLLL"
  3. Now, create the l array by repeating the l1 variable six times. Once again, you end up saving a lot of typing!

    2.2.2 :038 > l = [l1,l1,l1,l1,l1,l1,l7]
    => ["L        ", "L        ", "L        ", "L        ", "L        ", "L        ", "LLLLLLLLL"]
  4. Test the letter to make sure everything is formatted properly:

2.2.2 :039 > puts l
L
L
L
L
L
L
LLLLLLLLL
=> nil

Creating the letter O

The last letter array that you’ll need to spell out HELLO is the letter O. The shape of the letter O is similar to a circle or oval, and you can take advantage of that symmetry when creating your letter parts.

  1. Create variable o1 for the top of the letter:

    2.2.2 :040 > o1 = "   OOO   "
    => "   OOO   "
  2. Create o2:

    2.2.2 :041 > o2 = " O O "
    => "  O   O  "
  3. Create o3:

    2.2.2 :042 > o3 = " O     O "
    => " O     O "
  4. Variables o4 and o5 are just repeating o3:

    2.2.2 :043 > o4 = o3
    => " O     O "
    2.2.2 :044 > o5 = o3
    => " O     O "
  5. Variables o6 and o7 are the same as o2 and o1, respectively:

    2.2.2 :045 > o6 = o2
    => "  O   O  "
    2.2.2 :046 > o7 = o1
    => "   OOO   "
  6. Create the letter O array and test:

2.2.2 :047 > o = [o1,o2,o3,o4,o5,o6,o7]
=> ["   OOO   ", "  O   O  ", " O     O ", " O     O ", " O     O ", "  O   O  ", "   OOO   "]
2.2.2 :048 > puts o
   OOO
  O   O
 O     O
 O     O
 O     O
  O   O
   OOO
=> nil

Combining the letters into a word

Now it’s time to assemble HELLO. The first thing that comes to mind is to just use puts to print each array. puts can take a sequence of variables separated by commas.

Try printing your letters:

2.2.2 :049 > puts h, e, l, l, o
H       H
H       H
H       H
HHHHHHHHH
H       H
H       H
H       H
EEEEEEEEE
E
E
EEEEEEEEE
E
E
EEEEEEEEE
L
L
L
L
L
L
LLLLLLLLL
L
L
L
L
L
L
LLLLLLLLL
   OOO
  O   O
 O     O
 O     O
 O     O
  O   O
   OOO
=> nil

That sort of works, but it prints vertically. It would be nice if the letters were arranged horizontally to make it easy to read the word HELLO.

Remember that arrays are like boxes with compartments? Well, it turns out you can get the contents of any of those contents by asking for the compartment number like this:

2.2.2 :050 > h[0]
=> "H       H"
2.2.2 :051 > h[1]
=> "H       H"

Here, you’re providing the number of the compartment in square brackets next to the name of the array variable — h, in this case.

Follow these steps to get the letters to print horizontally:

  1. Combine the letters using string interpolation to access each array at the same time:

    2.2.2 :053 > puts "#{h[0]} #{e[0]} #{l[0]} #{l[0]} #{o[0]}"
    H       H EEEEEEEEE L         L            OOO
    => nil

    You can sort of see how the letters are lining up. The problem is that if you use puts on separate lines in IRB, it won’t look like the letter rows all connect. You need some way of repeating that command for each of the seven parts.

  2. A more advanced technique that you’ll use a lot in later projects is called looping.

    Looping is a way to have your code repeat itself a certain number of times. In Ruby, there is a handy looping method that you can call on numbers to count up to another number. Try this code:

    2.2.2 :055 > 0.upto(6) do |count|
    2.2.2 :056 >       puts h[count] + " " + e[count] + " " + l[count] + " " + l[count] + " " + o[count]
    2.2.2 :057? >   end

    As soon as you press Return or Enter after the end line, you should see:

    H       H EEEEEEEEE L         L            OOO
    H       H E         L         L           O   O
    H       H E         L         L          O     O
    HHHHHHHHH    EEEEEEEEE L         L          O     O
    H       H     E         L         L          O     O
    H       H E         L         L           O   O
    H       H EEEEEEEEE LLLLLLLLL  LLLLLLLLL   OOO
    => 0

    Success! The first line, 0.upto(6) do |count| starts the loop. It prepares Ruby to count starting at zero, up to and including six. As Ruby counts each number, it places the current number in the variable named count. Ruby then proceeds to the next line, which has your puts method. Inside of the string interpolation that combines all the letter parts, it asks for the zeroth one first, and prints that row. It then repeats six more times and prints each part in sequence (a total of seven). The final end line tells Ruby that the loop should stop there.