Learn

Methods are defined using the keyword def (short for “define”). Methods have three parts:

  1. The header, which includes the def keyword, the name of the method, and any arguments the method takes. (We’ll get to arguments in the next section)
  2. The body, which is the code block that describes the procedures the method carries out. The body is indented two spaces by convention (as with for, if, elsif, and else statements)
  3. The method ends with the end keyword.

Here’s an example of what the syntax would look like for a simple function, welcome, that just prints “Welcome to Ruby!” to the console:

def welcome puts "Welcome to Ruby!" end

Instructions

1.

We’ve started a method in the editor to the right, but it’s incomplete. Replace the ___s with the correct keywords so the method will work!

Take this course for free

By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Already have an account?