Simple Methods

A method is defined using the keywords def and end to frame a section of code:

def method_name    method codeend

The method’s name will primarily contain letters, numbers, and the underscore. As you’ll see, there are a few other special characters that may be part (or all) of the method’s name, but stick to alphanumeric characters and the underscore to start. Conventionally, method names always begin with a lowercase letter and use underscores to break up words: print_this, not printThis or PrintThis.

The functionality of the method, which is to say the code that will be executed when the method is called, goes between the method’s name and end.

Using just the information provided, there’s not much that a method can do, but let’s ...

Get Ruby: Visual Quickstart Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.