Ruby中的关键字如下:
模块定义:module
类定义: class
方法定义:def,undef
检查类型:defined?
条件语句:if,then,else,elsif,case,when,unless
循环语句:for,in,while,until,next,break,do,redo,retry,yield
逻辑判断:not,and,or
逻辑值: true,false
空值: nil
异常处理:rescue,ensure
对象引用:super,self
块的起始:begin/end
嵌入模块:BEGIN/END
文件相关:__FILE__,__LINE__
方法返回:return
别名: alias
条件判断
当判断条件为 "非false" 时执行特定语句
因为在Ruby中,除了false和nil外,都被认为是true
if 条件判断语句 [then]
......
end
或
dosomething if 条件
if 条件1 [then]
......
else
.....
end
if 条件1 [then]
......
elsif 条件2 [then] #注意else中没有字母e
......
else
......
end
if str.empty? [then]
puts 'str is empty...'
end
当判断条件为 "false"时执行特定语句
因为在Ruby中,false和nil都被认为是false
unless 条件判断语句 [then] #当为flase时执行
…...
end
或
Domsomething unless 条件判断语句
或
Domsomething if not 条件判断语句