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.
The script encoding of the current file.
The line number of this keyword in the current file.
The path to the current file.
Creates an alias between two methods (and other things).
Short-circuit Boolean and with lower precedence than &&
Starts an exception handling block.
Runs before any other code in the current file.
Leaves a block early.
Starts a case expression.
Creates or opens a class.
Defines a method.
Returns a string describing its argument.
Starts a block.
The unhandled condition in case, if and unless expressions.
An alternate condition for an if expression.
The end of a syntax block. Used by classes, modules, methods, exception handling and control expressions.
Runs after any other code in the current file.
Starts a section of code that is always run when an exception is raised.
A loop that is similar to using the each method.
Used for if and modifier if expressions.
Used to separate the iterable object and iterator variable in a for loop.
Creates or opens a module.
Skips the rest of the block.
A false value usually indicating “no value” or “unknown”.
Inverts the following boolean expression. Has a lower precedence than !
Boolean or with lower precedence than ||
Restarts execution in the current block.
Starts an exception section of code in a begin block.
Retries an exception block.
Exits a method.
The object the current method is attached to.
Calls the current method in a superclass.
Indicates the end of conditional blocks in control structures.
Prevents a class or module from responding to a method call.
Used for unless and modifier unless expressions.
Creates a loop that executes until the condition is true.
A condition in a case expression.
Creates a loop that executes while the condition is true.
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