How does the 'case' statement work with constants?
我正在使用Ruby1.9.2和RubyonRails3.2.2。我有以下方法:
1 2 3 4 5 6 7 8 9 | # Note: The 'class_name' parameter is a constant; that is, it is a model class name. def my_method(class_name) case class_name when Article then make_a_thing when Comment then make_another_thing when ... then ... else raise("Wrong #{class_name}!") end end |
我想知道为什么在上面的
我如何解决这个问题?有人建议如何处理这件事吗?
这是因为
mod === obj →true orfalse Case Equality—Returns
true ifobj is an instance ofmod or one ofmod ’s descendants. Of limited use for modules, but can be used in case statements to classify objects by class.
这意味着,对于除
相反,只需使用对象本身而不是其类来调用
将对象引用传递给方法,就像在后台一样,它使用==运算符,因此这些操作将失败。例如
1 2 3 4 5 6 7 8 9 | obj = 'hello' case obj.class when String print('It is a string') when Fixnum print('It is a number') else print('It is not a string') end |
另一方面,这样做很好:
1 2 3 4 5 6 7 8 9 | obj = 'hello' case obj # was case obj.class when String print('It is a string') when Fixnum print('It is a number') else print('It is not a string') end |
请参阅"如何在Ruby中编写switch语句"的相关答案https://stackoverflow.com/a/5694333/1092644
如果只想比较名称的相等性,可以将
1 2 3 4 5 6 7 8 9 10 11 | def my_method(class_name) case class_name.to_s when 'Article' make_a_thing when 'Comment' make_another_thing ... ... end end |