How to Use a Method in Ruby to Draw Reusable Rectangles

By Christopher Haupt

Methods (also called functions) in Ruby give you a way of storing and naming a piece of code and then using it later, possibly many times. You can pass different variables in to a method to change its behavior. You call variables passed to a method the method’s arguments (no, not the shouting kind!).

What if you wanted to draw two rectangles in a row? You could just copy the loop code and paste that code multiple times. Instead, you’re going to put the rectangle code into a method.

Follow these steps to create a reusable method that will draw rectangles:

  1. Start by adding a definition for the new rectangle method. Put this code at the top of your file:

    def rectangle(height, width, outside_letter, inside_letter)
      # The rectangle code will go here
    end

    The keyword def signals to Ruby that you’re about to provide the definition of a method. def is followed by the name of the method (rectangle) and then a list of zero or more arguments — each argument being the name of a variable you can use inside of the method. You next provide the code that makes up the method’s functionality and mark the end of the method with the keyword end.

  2. Select the entire rectangle drawing loop code, choose Edit→Cut, and then choose Edit→Paste to paste that code inside of the method in place of the comment shown in Step 1:

    def rectangle(height, width, outside_letter, inside_letter)
      1.upto(height) do |row|
        if row == 1
          puts outside_letter * width
        elsif row == height
          puts outside_letter * width
        else
          middle = inside_letter * (width - 2)
          puts "#{outside_letter}#{middle}#{outside_letter}"
        end
      end
    end
  3. Now you can use the method you’ve created to draw a rectangle. To do this, you can call the method (in Ruby, this is also referred to as sending a message). At the bottom of your source code, after the lines that set the width and height variables, write this code:

    rectangle(height, width, outside_letter, inside_letter)

    Note that the variable names you use to call a method don’t have to be named the same thing as what the arguments’ names are. Here, just to keep it simpler, they are the same. However, the position of the variables are important, and the first value you provide when calling a method goes into the first argument, the second into the second, and so on.

  4. Run the program. It should look like this:

    A successful rectangle.
    A successful rectangle.
  5. Copy and paste the rectangle method call so you have two exact copies of that line, and run the program again. What happens?

Putting your code into methods allows you to easily reuse the code and makes it easier to change it or fix bugs. Imagine if you had pasted the long set of code for drawing a rectangle, twice, three times, or many, many times. (Try it!) This works, but if you have to make a small change to your code, you have to hunt down every version wherever it might be. With a method, you’d only have to fix it once!