Table of contents
1.
Introduction
2.
Ruby
3.
Lexical Structure
4.
Identifiers in Ruby
5.
Keywords in Ruby
6.
Frequently Asked Questions
6.1.
What is Ruby?
6.2.
Ruby is said to be a combination of which five languages?
6.3.
What are Keywords in ruby?
6.4.
Is Ruby Case Sensitive?
6.5.
What does the keyword _FILE_ signify?
7.
Conclusion
Last Updated: Mar 27, 2024
Easy

Keywords in Ruby

Author Gaurav joshi
1 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Every product has a name and identifies itself with the same. The name of the product is its identity and value. Similarly, identifiers in ruby are nothing but just simply a name. Name to a variable, method, classes, etc., are all identifiers in ruby. But sometimes, a product cannot be called with certain words as these trademarks may be owned by some organisations or individuals, so no one else could use it.

In the same way, keywords in ruby are some reserved words that are reserved for doing some specific tasks. Users cannot have a keyword as an identifier; ruby trademarks it. The way Xbox trademark is owned by Microsoft so that no other manufacturer could name their product Xbox. Similarly, no identifiers could be named as a keyword.

In the below section, we will discuss keywords in ruby in detail but before that, let's get to know about ruby and lexical structure in ruby in brief.

Ruby

Ruby

Ruby 

Ruby is an open-source dynamic language with natural and easy-to-read/write syntax. Ruby is a language of careful balance and is a perfect blend of five different languages  (Perl, Smalltalk, Eiffel, Ada, and Lisp). Often, its creator  Yukihiro "Matz" Matsumoto considers ruby as a human being in the sense that the way human beings are simple from the outside but complex from the inside. Like human bodies, ruby is simple in appearance but complex on the inside. So I think you get to know in brief about ruby, so let us have a brief idea about lexical structure in Ruby before we go into details to understand keywords in ruby.

Lexical Structure

Like Human Languages, computer languages also follow a lexical structure. A source code of the ruby program consists of tokens. While parsing any program, the ruby-interpreter parses the program as a sequence of these tokens. Tokens in ruby include literal, punctuation, comments, identifiers, and keywords. This article introduces you to one type of token, i.e., Keywords in ruby. Tokens also include essential information about the characters that comprise the tokens and the whitespace that separates the tokens. 

Identifiers in Ruby

An identifier in ruby is simply a name. Ruby uses these identifiers to name variables, classes, methods, etc. Identifiers in ruby consist of letters, numbers, and underscore characters, but they do not begin with a number. Also, Identifiers in ruby do not include whitespace or nonprinting characters or punctuation characters except as described here. 

Let us look into some sample identifiers. 

  • i  
  • X2  
  • old_value  # multiword identifiers in ruby that are not constant
  • _internal    # Identifiers may begin with underscores
  • PI   # Constant
  • NEW_VALUE  # multiword identifiers in ruby for constant

Keywords in Ruby

As we have already stated in the introduction, Keywords in ruby are nothing but reserved words used for some specific internal process or represent some predefined actions. No variable Object or constant could be named a keyword. Users cannot have a keyword as an identifier. Doing this will give a compile-time error in ruby. E.g. we will get a compile-time error by running the below code since it uses the keyword "if" as a prohibited variable.

Code:- 

# Program to illustrate Keywords in ruby
  
# !/usr/bin/ruby
  
# "if" is a keyword in ruby, so the user can't use it as a variable
if = 20
  
# Here, 'if' and 'end' are keywords in ruby 
# if the condition is used to check whether user age is enough for voting 
if if >= 18
  puts "User eligible to vote."
end
You can also try this code with Online Ruby Compiler
Run Code


Error:-

 

 

Now we will see all 41 keywords in ruby and what are there internal functions they perform. 

KEYWORDS IN RUBY

DESCRIPTION

BEGIN Code inside BEGIN will run before any other code written in the current file.
END Code inside END will run after any other code written in the current file
alias It helps to create an alias between two methods
and It has lower precedence than && and is for short-circuit boolean
begin It helps us to start an exception handling block
break The break helps to leave a block early
case It helps to start a case expression
class Helps user to create or opens a class in ruby
def def keyword in ruby helps us to define a function
defined? Defined? It describes its argument by returning a string 
do It helps to start a block of code
else Else is the unhandled condition in if and unless expression
elsif Elsif helps the user to give alternate conditions to if expression
end methods, modules, classes etc, use end. It indicates the end of a syntax block
false False here means boolean false 
ensure ensure is a block of code that will always run when an exception is raised
for To help us to create a loop for iteration
if Used as an expression for if or modifiers if
in It helps to separate iterable Objects and iterable variables in a for loop
module Helps us in creating or opening module
next  Lets the user skips the rest of the code in the block
nil Nil represents false value indicating "no value" or "unknown."
not It inverts the boolean expression but has less precedence than "!"
or It represents boolean or with less precedence than "||"
redo Redo helps to restart execution in the present block
rescue It helps to start an exception section of code in a beginning block
retry It helps us to retries an exception block
return To exit from a method
self Helps in calling Object of current method attached to 
super It helps in calling the current method in a superclass
then In control structure, It represents the end of the conditional block
true true here is just a simple boolean true expression
undef It helps in preventing module or class from responding to the method call
unless It helps in creating unless and modifiers unless expressions
until Help us to create an iterative loop until a condition is met
when it helps by representing a condition in a case expression
while Help us to create an iterative loop while a condition is true
yield yield Helps to start execution of block send to the current method
_ENCODING_ It represents script encoding of the current file
_LINE_ It represents the line number of keywords in the current file
_FILE_ It represents the path to the current file.

Frequently Asked Questions

What is Ruby?

Ruby is an open-source dynamic language with natural and easy-to-read/write syntax. Ruby is a language of careful balance and is a perfect blend of five different languages.

Ruby is said to be a combination of which five languages?

Ruby, an open-source dynamic language, is a perfect blend of five different languages (Perl, Smalltalk, Eiffel, Ada, and Lisp).

What are Keywords in ruby?

Keywords in ruby are nothing but reserved words used for some specific internal process or represent some predefined actions.No variable, Object, or constant could be named a keyword.

Is Ruby Case Sensitive?

Ruby is a case-sensitive language, which means lowercase identifiers in ruby are different from uppercase identifiers. E.g. Like_This is entirely different from LIKE_THIS. Similarly, Start is different from START.

What does the keyword _FILE_ signify?

Keyword _FILE_ helps us represent the current file's path.

Conclusion

In this article, we have studied in detail the Keywords in Ruby and briefly explored all the Keywords in Ruby. 

Also, visit our Guided Path in  Coding Ninjas Studio to learn about  Ruby. If you are preparing for an interview, visit our Interview Experience Section and interview bundle for placement preparations. Upskill yourself in Ruby on RailsBackend Web TechnologiesHadoopSQL, MongoDB, Data Structures and Algorithms, JavaScript,  System Design and much more!. 
To learn more, refer to Operational DatabasesNon- relational databasesMongoDBTop-100-SQL-problemsinterview-experienceIntroduction to ruby on railsDirectory Structure in RubyRuby on RailsRuby vs Python. You can also refer to the Official RubyOfficial DocumentationRuby Koans, and Official Ruby FAQ.

We expect that this article must have helped you enhance your knowledge on the topic of Ruby. Please upvote our blogs if you find them engaging and helpful! 

We wish you all the best for your future adventures and Happy Coding. 

Conclusion

Live masterclass