带字符串的ruby case语句不起作用(如预期的那样)

时间:2017-04-24 23:51:32

标签: ruby string switch-statement

result_list
#> # A tibble: 50 × 3
#>           page                   date
#>          <chr>                  <chr>
#> 1  Seite 2/490                   <NA>
#> 2  Seite 1/490 Sendung vom 24.04.2017
#> 3  Seite 1/490 Sendung vom 21.04.2017
#> 4  Seite 1/490 Sendung vom 20.04.2017
#> 5  Seite 1/490 Sendung vom 19.04.2017
#> 6  Seite 1/490 Sendung vom 18.04.2017
#> 7  Seite 1/490 Sendung vom 15.04.2017
#> 8  Seite 1/490 Sendung vom 13.04.2017
#> 9  Seite 1/490 Sendung vom 12.04.2017
#> 10 Seite 1/490 Sendung vom 11.04.2017
#> # ... with 40 more rows, and 1 more variables: text <chr>

这不匹配。

我也试过了:

  1. case 'this is not a test' when 'this is not a test'.eql?('this is not a test') p '1' end 并正确更改my_var = 'this is not a test'语句。
  2. 做#1加上各种case次尝试。
  3. 打印所有内容以查看是否有任何东西返回任何奇怪的内容(无等)
  4. 使用IRB测试1,2和3。

1 个答案:

答案 0 :(得分:4)

当然不匹配。

  • 'this is not a test'.eql?('this is not a test')评估为true

  • case x when y .... end评估为if y === x ... end

  • true === 'this is not a test'评估为false

您要么case when x ... end,评估为if x ... end,语法略有不同,语义不同:

case
  when 'this is not a test'.eql?('this is not a test')
    p '1' 
end

或者,如果您对===而不是eql?感到满意,可以写

case 'this is not a test'
  when 'this is not a test'
    p '1' 
end