Ruby on Rails开关

有人可以提供一个例子,如何在Ruby中使用开关大小写variables?

我假设你参考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语句中使用正则expression式。

 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}"