Cover by Michael Fitzgerald

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

O'Reilly logo

Ruby's Reserved Words

Every programming language has its own list of reserved words (aka keywords), which are reserved for its own purposes so that it can do its job. They are the words that make statements in programs, and without statements, or instructions, how could a program tell a computer what to do?

Table 2-1 lists Ruby's reserved words and briefly describes the purpose of each.

Table 2-1. Ruby's reserved words

Reserved word

Description

BEGIN

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

END

Code, enclosed in { and }, 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. (Compare with or.)

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 if 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, in if, elsif, unless, or when, is not true.

elsif

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

end

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

ensure ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required