Ruby | Keywords (original) (raw)
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 `
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:
Keyword | Description |
---|---|
__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. |
BEGIN | Runs before any other code in the current file. |
END | Runs after any other code in the current file. |
alias | Creates an alias between two methods (and other things). |
and | Short-circuit Boolean and with lower precedence than && |
begin | Starts an exception handling block. |
break | Leaves a block early. |
case | Starts a case expression. |
class | Creates or opens a class. |
def | Defines a method. |
defined? | Returns a string describing its argument. |
do | Starts a block. |
else | The unhandled condition in case, if and unless expressions. |
elsif | An alternate condition for an if expression. |
end | The end of a syntax block. Used by classes, modules, methods, exception handling and control expressions. |
ensure | Starts a section of code that is always run when an exception is raised. |
false | Boolean false. |
for | A loop that is similar to using the each method. |
if | Used for if and modifier if expressions. |
in | Used to separate the iterable object and iterator variable in a for loop. |
module | Creates or opens a module. |
next | Skips the rest of the block. |
nil | A false value usually indicating “no value” or “unknown”. |
not | Inverts the following boolean expression. Has a lower precedence than ! |
or | Boolean or with lower precedence than | |
redo | Restarts execution in the current block. |
rescue | Starts an exception section of code in a begin block. |
retry | Retries an exception block. |
return | Exits a method. |
self | The object the current method is attached to. |
super | Calls the current method in a superclass. |
then | Indicates the end of conditional blocks in control structures. |
true | Boolean true. |
undef | Prevents a class or module from responding to a method call. |
unless | Used for unless and modifier unless expressions. |
until | Creates a loop that executes until the condition is true. |
when | A condition in a case expression. |
while | Creates a loop that executes while the condition is true. |
yield | Starts execution of the block sent to the current method. |
Example:
Ruby `
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