Ruby基础知识-条件判断语句、case when

条件判断语句与其他语言差不多,这里不再举例子

一. 单行 if(如果) 语句

1)
if 条件① then 语句1; 语句2 ; 语句… end
2)
( 语句1; 语句2 ; 语句… ) if 条件
二. 多行 if 语句
if 条件
语句1; 语句2 ; 语句…
elsif 条件
语句1; 语句2 ; 语句…
else
语句1; 语句2 ; 语句…
end
三. unless(除非) 条件语句:
unless 条件 = if not (条件)

① Ruby里,nil 和 false 为假,其它都为真; puts "is true" if 5 #is true

str="false" ; puts "is true" if str #is true


我们看下case when

x=3
case x
when 1..2
print "x=",x,";inside 1..2 "
when 4..9, 0
print "x=",x,";inside 4..9,0,map be 0"
else
print "x=",x,";other"
end


猜你喜欢

转载自blog.csdn.net/SDN_SUPERUSER/article/details/37743911