娃娃鸭学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