Category ROR

Keywords or reserved words in Ruby or any other programming language for that matter, are words that have a standard pre-defined meaning and have been reserved for executing specific tasks. These words have the same meaning in all Ruby programs.

As these words have been reserved from use, for an internal process and represent predefined actions, these keywords cannot be used for any other purpose, for example, naming variables, objects, or constants in Ruby. Using these keywords may result in compile-time error.

Here is a list of reserved words in Ruby programming to help you write bug-free code.

__ENCODING__
The script encoding of the current file.

__LINE__
The line number of this keyword in the current file.

__FILE__
The path to the current file.

alias
Creates an alias between two methods (and other things).

and
Short-circuit Boolean and with lower precedence than &&

begin
Starts an exception handling block.

BEGIN
Runs before any other code in the current file.

break
Leaves a block early.

case
Starts a case expression.

class
Creates or opens a class.

def
Defines a method.

defined?
Returns a string describing its argument.

do
Starts a block.

else
The unhandled condition in case, if and unless expressions.

elsif
An alternate condition for an if expression.

end
The end of a syntax block. Used by classes, modules, methods, exception handling and control expressions.

END
Runs after any other code in the current file.

ensure
Starts a section of code that is always run when an exception is raised.

false
Boolean false.

for
A loop that is similar to using the each method.

if
Used for if and modifier if expressions.

in
Used to separate the iterable object and iterator variable in a for loop.

module
Creates or opens a module.

next
Skips the rest of the block.

nil
A false value usually indicating “no value” or “unknown”.

not
Inverts the following boolean expression. Has a lower precedence than !

or
Boolean or with lower precedence than ||

redo
Restarts execution in the current block.

rescue
Starts an exception section of code in a begin block.

retry
Retries an exception block.

return
Exits a method.

self
The object the current method is attached to.

super
Calls the current method in a superclass.

then
Indicates the end of conditional blocks in control structures.

true
Boolean true.

undef
Prevents a class or module from responding to a method call.

unless
Used for unless and modifier unless expressions.

until
Creates a loop that executes until the condition is true.

when
A condition in a case expression.

while
Creates a loop that executes while the condition is true.

yield
Starts execution of the block sent to the current method.

A key consideration while naming classes and modules, is to always check not to use a name that is identical to an existing class.

It’s quite easy to get caught up with naming conventions. Here are a few tips to take into account when you stumble upon using reserved words for your application;

  • Try using synonyms
  • Add prefix on model
  • Add pseudo keyword to the attribute

This article is written by RubyConf Pakistan's community lead, Sabrina Malik.