case nwhen 0puts 'You typed zero'when 1, 9puts 'n is a perfect square'when 2puts 'n is a prime number'puts 'n is an even number'when 3, 5, 7puts 'n is a prime number'when 4, 6, 8puts 'n is an even number'elseputs 'Only single-digit numbers are allowed'end
另一个例子:
score = 70
result = case scorewhen 0..40 then "Fail"when 41..60 then "Pass"when 61..70 then "Pass with Merit"when 71..100 then "Pass with Distinction"else "Invalid Score"end
puts result
在我的Kindle上Ruby编程语言(第一版,O'Reilly)的第123页左右,它说when子句后面的then关键字可以替换为换行符或分号(就像if then else语法一样)。(Ruby 1.8也允许冒号代替then,但Ruby 1.9不再允许这种语法。)
case xwhen 1..5"It's between 1 and 5"when 6"It's 6"when "foo", "bar""It's either foo or bar"when String"You passed a string"else"You gave me #{x} -- I have no idea what to do with that."end
obj = 'hello'case obj # was case obj.classwhen Stringprint('It is a string')when Fixnumprint('It is a number')elseprint('It is not a string or number')end
case awhen 1puts "Single value"when 2, 3puts "One of comma-separated values"when 4..6puts "One of 4, 5, 6"when 7...9puts "One of 7, 8, but not 9"elseputs "Any other thing"end
不带参数:
casewhen b < 3puts "Little than 3"when b == 3puts "Equal to 3"when (1..10) === bputs "Something in closed range of [1..10]"end
case foowhen /^(true|false)$/puts "Given string is boolean"when /^[0-9]+$/puts "Given string is integer"when /^[0-9\.]+$/puts "Given string is float"elseputs "Given string is probably string"end
is_even = ->(x) { x % 2 == 0 }
case numberwhen 0 then puts 'zero'when is_even then puts 'even'else puts 'odd'end
您还可以使用带有自定义===的结构轻松创建自己的比较器
Moddable = Struct.new(:n) dodef ===(numeric)numeric % n == 0endend
mod4 = Moddable.new(4)mod3 = Moddable.new(3)
case numberwhen mod4 then puts 'multiple of 4'when mod3 then puts 'multiple of 3'end
print "Enter a string: "some_string = gets.chompcasewhen some_string.match(/\d/)puts 'String has numbers'when some_string.match(/[a-zA-Z]/)puts 'String has letters'elseputs 'String has no numbers or letters'end
# Define the hashmenu = {a: :menu1, b: :menu2, c: :menu2, d: :menu3}
# Define the methodsdef menu1puts 'menu 1'end
def menu2puts 'menu 2'end
def menu3puts 'menu3'end
# Let's say we case by selected_menu = :aselected_menu = :a
# Then just call the relevant method from the hashsend(menu[selected_menu])
puts "Recommend me a language to learn?"input = gets.chomp.downcase.to_s
case inputwhen 'ruby'puts "Learn Ruby"when 'python'puts "Learn Python"when 'java'puts "Learn Java"when 'php'puts "Learn PHP"else"Go to Sleep!"end
class Successdef self.===(item)item.status >= 200 && item.status < 300endend
class Emptydef self.===(item)item.response_size == 0endend
case http_responsewhen Emptyputs "response was empty"when Successputs "response was a success"end
code = '!ADD-SUPER-BONUS!'
class StrContainsdef self.===(item)item.include? 'SUPER' or item.include? 'MEGA' or\item.include? 'MINI' or item.include? 'UBER'endend
case code.upcasewhen '12345PROMO', 'CODE-007', StrContainsputs "Code #{code} is a discount code!"when '!ADD-BONUS!'puts 'This is a bonus code!'elseputs 'Sorry, we can\'t do anything with the code you added...'end
x = 22
CASE xWHEN 0..14 THEN puts "#{x} is less than 15"WHEN 15 THEN puts "#{x} equals 15"WHEN 15 THEN puts "#{x} equals 15"WHEN 15..20 THEN puts "#{x} is greater than 15"ELSE puts "Not in the range, value #{x} "END
case expressionwhen constant1, constant2 #Each when statement can have multiple candidate values, separated by commas.# statementsnext # is like continue in other languageswhen constant3# statementsexit # exit is like break in other languages...else# statementsend
例如:
x = 10case xwhen 1,2,3puts "1, 2, or 3"exitwhen 10puts "10" # it will stop here and execute that lineexit # then it'll exitelseputs "Some other number"end