Methods ↑

Methods implement the functionality of your program. Here is a simple method definition:

def one_plus_one
  1 + 1
end

A method definition consists of the def keyword, a method name, the body of the method, return value and the end keyword. When called the method will execute the body of the method. This method returns 2.

Since Ruby 3.0, there is also a shorthand syntax for methods consisting of exactly one expression:

def one_plus_one = 1 + 1

This section only covers defining methods. See also the syntax documentation on calling methods.

Method Names ↑