Skip to content
Related Articles

Related Articles

Improve Article

Ruby | Keywords

  • Last Updated : 26 Oct, 2018

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.

Example:




# Ruby program to illustrate Keywords 
  
#!/usr/bin/ruby
  
# 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."
end

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:

KeywordDescription
__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.

Example:




# Ruby program to illustrate the use of Keywords 
  
#!/usr/bin/ruby 
     
# 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
     
# end of class GFG 
# using 'end' keyword
end
     
# creating object 
obj = GFG.new
     
# calling method using object 
obj.geeks 

Output:

Hello Geeks!!

Reference: http://ruby-doc.org/docs/keywords/1.9/Object.html




My Personal Notes arrow_drop_up
Recommended Articles
Page :