Ruby条件范围

范围也可以用作条件表达式。例如,下面的代码片段从标准输入打印行,其中每个集合的第一行包含单词 start,最后一行包含单词 end:

while gets print if /start/../end/ end

案例

范围用在case中

范围可以用在 case 语句中

#!/usr/bin/ruby -w # -*- coding : utf-8 -*- puts "HaiCoder(www.haicoder.net)" score = 70 result = case score when 0..40 "糟糕的分数" when 41..60 "快要及格" when 61..70 "及格分数" when 71..100 "良好分数" else "错误的分数" end puts result

程序运行后,控制台输出如下:

03_Ruby条件范围.png

范围可以用在 case 语句中。