娃娃鸭学Ruby-28、Case

Case

name= case
where x==1 then "one"
where x==2 then "two"
where x==3 then "three"
where x==4 then "four"
else "many"
end
同if

case
where x==1
"one"
where x==2
"two"
where x==3
"three"
end

case
where x==1,y==0 then "x is one or y is zero"
where x==2||y==1 then "x is two or y is one "
end

name=case x
when 1
"one"
when 2 then "two"
when 3; "three"
else "many"
end

name= case
when 1===x then "one"
when 2===x then "two"
when 3===x then "three"
else "many"
end
===是条件相等性操作符

puts case x
when String then "string"
when Numeric then "number"
when TrueClass,FalseClass then "boolean"
else "other"
end

tax=case income
when 0..7550
income *0.1
when 7500..30650
755+(income-7550)*0.15
when 30650..742000
4220 +(income-30655)*0.25
when 74200..154800
15107.5+(income-74201)*0.28
when 154800..336550
37675.5+(income-154800)*0.33
else
97653 +(income-336550)*0.35
end

when line=gets.chomp do
case line
when /^\s*#/
next
when /^quit$/i
break
else
puts line.reverse
end
end

def hasValue?(x)
case x
when nil,[],"",0 # if nil===x || []===x || ""===x || 0===x then
false
else
true
end
end

2011-4-19 13:28 danny
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值