Reserved Words

Table 1 lists Ruby’s reserved words or keywords.

Table 1. Ruby’s reserved words

Reserved word

Description

BEGIN

Code, enclosed in { }, to run before the program runs.

END

Code, enclosed in { }, to run when the program ends.

alias

Creates an alias for an existing method, operator, or global variable.

and

Logical operator; same as && except and has lower precedence.

begin

Begins a code block or group of statements; closes with end.

break

Terminates a while or until loop, or a method inside a block.

case

Compares an expression with a matching when clause; closes with end. See when.

class

Defines a class; closes with end.

def

Defines a method; closes with end.

defined?

A special operator that determines whether a variable, method, super method, or block exists.

do

Begins a block, and executes code in that block; closes with end.

else

Executes following code if previous conditional is not true, set with if, elsif, unless, or case. See if, elsif.

elsif

Executes following code if previous conditional is not true, set with if or elsif.

end

Ends a code block (group of statements) started with begin, class, def, do, if, etc.

ensure

Always executes at block termination; use after last rescue.

false

Logical or Boolean false; instance of FalseClass; a pseudovariable. See true.

for

Begins a for loop; used with in.

if

Executes code block if conditional statement is true. Closes with end. Compare unless, until.

in

Used with for loop. See for.

module

Defines a module; closes with end.

next

Jumps to the point immediately before ...

Get Ruby Pocket Reference 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.