【发布时间】:2016-06-27 10:18:18
【问题描述】:
通常可以像这样比较两个类
x = "String"
puts "A string" if x.class == String
但是,当case这样使用时,
x = "String"
case x.class
when String
puts "A string"
end
它不起作用。为什么?
更新:
以下情况,
x = "String"
case x
when String
puts "A string"
end
它有效。是不是说case隐式地把类转换成字符串?
【问题讨论】:
-
您在问
x的class是否是String。好吧,显然不是,它是Class。在第二个示例中,您要问x是否是String,就是这样。
标签: ruby