Keywords are special words in Ruby which are reserved by the language and have predefined uses. Each keyword serves a different implementation than other. Let’s dive into these keywords and understand their definitions.
- BEGIN -
- Code inside BEGIN will run before any other code written in the current file.
- 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
- Code inside END will run after any other code written in the current file
- alias -
- alias helps to create an alias between two methods and It has lower precedence than && and is for short-circuit boolean
- alias helps to create an alias between two methods and It has lower precedence than && and is for short-circuit boolean
- begin -
- This Ruby keyword helps us to start an exception handling block
- This Ruby keyword helps us to start an exception handling block
- break -
- It helps to leave a block early
- It helps to leave a block early
- case -
- This Ruby keyword helps to start a case expression
- This Ruby keyword helps to start a case expression
- class -
- class helps user to create or opens a class in ruby
- class helps user to create or opens a class in ruby
- def -
- def keyword in ruby helps us to define a function
- def keyword in ruby helps us to define a function
- defined? -
- Defined? It describes its argument by returning a string
- Defined? It describes its argument by returning a string
- do -
- This Ruby keyword helps to start a block of code
- This Ruby keyword helps to start a block of code
- else -
- Else is the unhandled condition in if and unless expression
- Else is the unhandled condition in if and unless expression
- elsif -
- Elsif helps the user to give alternate conditions to if expression
- 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
- 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
- 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
- To help us to create a loop for iteration
- if -
- Used as an expression for if or modifiers if
- Used as an expression for if or modifiers if
- in -
- It helps to separate iterable Objects and iterable variables in a for loop
- It helps to separate iterable Objects and iterable variables in a for loop
- module -
- Helps us in creating or opening module
- Helps us in creating or opening module
- next -
- Lets the user skips the rest of the code in the block
- Lets the user skips the rest of the code in the block
- nil -
- Nil represents false value indicating “no value” or “unknown.”
- Nil represents false value indicating “no value” or “unknown.”
- not -
- It inverts the boolean expression but has less precedence than “!”
- It inverts the boolean expression but has less precedence than “!”
- or -
- It represents boolean or with less precedence than “||”
- It represents boolean or with less precedence than “||”
- redo -
- Redo helps to restart execution in the present block
- Redo helps to restart execution in the present block
- rescue -
- It helps to start an exception section of code in a beginning block
- It helps to start an exception section of code in a beginning block
- retry -
- It helps us to retries an exception block
- It helps us to retries an exception block
- return -
- To exit from a method
- To exit from a method
- self -
- Helps in calling Object of current method attached to
- Helps in calling Object of current method attached to
- super -
- It helps in calling the current method in a superclass
- It helps in calling the current method in a superclass
- then -
- In control structure, It represents the end of the conditional block
- In control structure, It represents the end of the conditional block
- true -
- true here is just a simple boolean true expression
- true here is just a simple boolean true expression
- undef -
- It helps in preventing module or class from responding to the method call
- It helps in preventing module or class from responding to the method call
- unless -
- It helps in creating unless and modifiers unless expressions
- It helps in creating unless and modifiers unless expressions
- until -
- Help us to create an iterative loop until a condition is met
- Help us to create an iterative loop until a condition is met
- when -
- it helps by representing a condition in a case expression
- it helps by representing a condition in a case expression
- while -
- Help us to create an iterative loop while a condition is true
- 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
- yield Helps to start execution of block send to the current method
- ENCODING -
- It represents script encoding of the current file
- It represents script encoding of the current file
- LINE -
- It represents the line number of keywords in the current file
- It represents the line number of keywords in the current file
- FILE -
- It represents the path to the current file.
Happy Coding!!!