Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App

Related Articles

Ruby | Keywords

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

Keywords or Reserved words are the words in a language that are used for some internal process or represent some predefined actions. These words are therefore not allowed to use as variable names or objects or as constants. Doing this may result in compile-time error.


# Ruby program to illustrate Keywords 
# here 'if' is a keyword
# it can't be used as variable
if = 20
# Here 'if' and 'end' are keywords.  
# if condition to check whether  
# your age is enough for voting 
if if >= 18
  puts "You are eligible to vote."

Compile Time Error:

Error(s), warning(s):

source_file.rb:7: syntax error, unexpected ‘=’
if = 20
source_file.rb:12: syntax error, unexpected >=
if if >= 18
source_file.rb:14: syntax error, unexpected keyword_end, expecting end-of-input

There are total 41 keywords present in Ruby as shown below:

__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.
BEGINRuns before any other code in the current file.
ENDRuns after any other code in the current file.
aliasCreates an alias between two methods (and other things).
andShort-circuit Boolean and with lower precedence than &&
beginStarts an exception handling block.
breakLeaves a block early.
caseStarts a case expression.
classCreates or opens a class.
defDefines a method.
defined?Returns a string describing its argument.
doStarts a block.
elseThe unhandled condition in case, if and unless expressions.
elsifAn alternate condition for an if expression.
endThe end of a syntax block. Used by classes, modules, methods, exception handling and control expressions.
ensureStarts a section of code that is always run when an exception is raised.
falseBoolean false.
forA loop that is similar to using the each method.
ifUsed for if and modifier if expressions.
inUsed to separate the iterable object and iterator variable in a for loop.
moduleCreates or opens a module.
nextSkips the rest of the block.
nilA false value usually indicating “no value” or “unknown”.
notInverts the following boolean expression. Has a lower precedence than !
orBoolean or with lower precedence than ||
redoRestarts execution in the current block.
rescueStarts an exception section of code in a begin block.
retryRetries an exception block.
returnExits a method.
selfThe object the current method is attached to.
superCalls the current method in a superclass.
thenIndicates the end of conditional blocks in control structures.
trueBoolean true.
undefPrevents a class or module from responding to a method call.
unlessUsed for unless and modifier unless expressions.
untilCreates a loop that executes until the condition is true.
whenA condition in a case expression.
whileCreates a loop that executes while the condition is true.
yieldStarts execution of the block sent to the current method.


# Ruby program to illustrate the use of Keywords 
# defining class Vehicle 
# using the 'class' keyword
class GFG
# defining method 
# using 'def' keyword
def geeks 
    # printing result 
    puts "Hello Geeks!!"
# end of the method 
# using 'end' keyword
# end of class GFG 
# using 'end' keyword
# creating object 
obj =
# calling method using object 


Hello Geeks!!


My Personal Notes arrow_drop_up
Last Updated : 26 Oct, 2018
Like Article
Save Article
Similar Reads