How to Create Code to Draw a Triangle in Ruby

By Christopher Haupt

You can create a method to draw a triangle in Ruby. First, think about how this might work. The triangle you’ll draw will look like an isosceles triangle, where two sides will be the same size and the base will appear slightly smaller.

Unlike the rectangle, where each row is easy to format, for the triangle you need to make each row look different. The first row will be the top of the triangle (the pointy end). The last row will be the base of the triangle and will be the width that the user specifies.

See if you can figure out what the code is doing here.

  1. Start a new method called triangle:

    # Above here is the end of the rectangle method
    def triangle( height, outside_letter, inside_letter)
    # Code for the triangle will go here
    end

    Note that you’ll be using the height variable for both the height and the width inside of this method.

  2. Create a loop that will repeat height times. Put this code inside of the triangle method:

    1.upto(height) do |row|
    # Drawing code goes here in the next step
    end
  3. For a triangle, you need to draw whitespace (empty areas) for each row that doesn’t take up the entire width that you’re drawing. As you draw each row, you’ll be drawing less whitespace. Add this line as the first line of your loop:

    print ' ' * (height - row)

    The math here will calculate a smaller number of spaces as the number of the row gets larger (remember, you’re counting row 1 at the top, and row will equal the height at the bottom).

  4. Next, you have to handle the case for the first row, which is the top of the triangle:

    if row == 1
      puts "#{outside_letter * 2}"
    end

    Step 4’s code goes immediately after Step 3.

  5. Handle the last row case next by adding an elsif condition.

    if row == 1
      puts "#{outside_letter * 2}"
    elsif row == height
      puts outside_letter * height * 2
    end
  6. Now add the code for the slightly more complicated case of handling all the middle rows. For this last part of the condition, you’ll use an else clause. See the entire condition here:

    if row == 1
      puts "#{outside_letter * 2}"
    elsif row == height
      puts outside_letter * height * 2
    else
      middle = inside_letter * (row - 2)
      print "#{outside_letter}#{middle}#{inside_letter}"
      puts "#{inside_letter}#{middle}#{outside_letter}"
    end

    The code looks a little strange. Why is there both a print and a puts statement?

  7. It’s time to display the triangle. At the very bottom of the code file, beneath the rectangle method call, add a triangle method call:

    triangle( height, outside_letter, inside_letter)
  8. Save your program file, switch to your terminal, and run the program. You should see something like this:

    The example code used includes a rectangle method prior to the triangle method, so you'll see a rec

    The example code used includes a rectangle method prior to the triangle method, so you’ll see a rectangle balanced on a triangle.