Ruby on Rails开关
这个问题在这里已经有了答案:
我假设你参考case / when。
case a_variable # a_variable is the variable we want to compare
when 1 #compare to 1
puts "it was 1"
when 2 #compare to 2
puts "it was 2"
else
puts "it was something else"
end
要么
puts case a_variable
when 1
"it was 1"
when 2
"it was 2"
else
"it was something else"
end
编辑
可能不是每个人都知道的东西,但是可以非常有用的是,您可以在case语句中使用正则表达式。
foo = "1Aheppsdf"
what = case foo
when /^[0-9]/
"Begins with a number"
when /^[a-zA-Z]/
"Begins with a letter"
else
"Begins with something else"
end
puts "String: #{what}"
链接地址: http://www.djcxy.com/p/25635.html
上一篇: Ruby on Rails Switch
下一篇: codeabbey task