いくつかのswitch文

switch文を拡張した、新たなswitchマクロがバージョン0.7から追加されました。

■正規表現による文字列のスイッチ文

            def str = “Curl is great, I like Curl”
            {regexp-switch str
             case “^Curl.*Curl$” do
                {output strは正規表現^Curl.*Curl$に一致。}
             case “^Java.*Java$”, “^Ruby.*Ruby$” do
                {output strは正規表現^Java.*Java$もしくは^Ruby.*Ruby$に一致。}
             else
                {output str}
            }

■文字列の先頭文字によるスイッチ文(この逆でsuffix-switchもあります。)

            def str = “abcdef”
            {prefix-switch str, ignore-case? = true
             case “abc” do
                {output strはabcから始まります。}
             case “xyz”, “xxx” do
                {output strはxyzもしくはxxxから始まります。}
             else
                {output str}
            }

■数値の範囲によるスイッチ文

            def i = 50
            def result =
                {range-switch i
                    case 0 to 20 do “Too bad”
                    case 26 to 50 do “Bad”
                    case 51 to 75 do “Not bad”
                    case 75 to 99 do “Good”
                    case 100 do “Excellent”
                    case 101 to 200, 400 to 600 do “Strange!?”
                    else {unreachable}
                }
            {output result} || ==> “Bad”

注意)バージョン0.7からサポートされます。