Ruby-条件判断
=begin条件判断语句包括if语句,unless语句,case语句,=end#if语句=beginif条件then处理1elsif条件2then处理2else处理3end=enda=10b=20ifa>bputs"abiggerthanb"elsifa<bputs"asmallerthanb"elseputs"aisequaltob"endputs"abiggerthanb"ifa>b#unless语句,与if语句相反,条件判断为假的时执行处理=beginunless条件处理1else处理2end=enda=10b=20unlessa>bputs"asmaller/sameasthanb"end#case语句,适用于比较的对象只有一个的时候,根据这个对象的值不同,执行不同的处理;when可以一次指定多个值=begincase比较对象when值1then处理1when值2then处理2when值3then处理3else处理4end=endtags=["A","IMG","PRE"]tags.eachdo|word|casewordwhen"P","A","I","B","BLOCKQUOTE"puts"#{word}haschild."when"IMG","BR"puts"#{word}haschild."elseputs"#{word}cannootbeused"endendarray=["a",1,nil]array.eachdo|word|casewordwhenStringputs"thewordisaString"whenNumericputs"thewordisaNumeric"elseputs"otherwordsareSomething"endendtext.each_linedo|line|caselinewhen/^From:/iputs"findasender"when/^To:/iputs"findareceiver"when/^$/puts"resolutionarefinnished"exitelsebreakendend
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。