红宝石 |关键词
关键字或保留字是一种语言中用于某些内部流程或表示某些预定义操作的字词。因此,这些词不允许用作变量名或对象或常量。这样做可能会导致编译时错误。
例子:
# 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
编译时错误:
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
Ruby 中共有 41 个关键字,如下所示:
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. |
例子:
# 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
输出:
Hello Geeks!!
参考: http://ruby-doc.org/docs/keywords/1.9/Object.html