📜  红宝石 |关键词

📅  最后修改于: 2022-05-13 01:54:34.331000             🧑  作者: Mango

红宝石 |关键词

关键字或保留字是一种语言中用于某些内部流程或表示某些预定义操作的字词。因此,这些词不允许用作变量名或对象或常量。这样做可能会导致编译时错误。

例子:

# 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

编译时错误:

Ruby 中共有 41 个关键字,如下所示:

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.

例子:

# 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