Ruby类类型和case语句 [英] Ruby class types and case statements

查看:75
本文介绍了Ruby类类型和case语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

两者之间有什么区别

case item.class
when MyClass
  # do something here
when Array
  # do something different here
when String
  # do a third thing
end

case item.class
when MyClass.class
  # do something here
when Array.class
  # do something different here
when String.class
  # do a third thing
end

由于某种原因,其中的第一个有时起作用而第二个不起作用,而有时,第二个起作用而第一个不起作用.为什么?哪种方法是正确"的方法?

For some reason, the first one of these works sometimes and the second doesn't, and other times, the second one works and the first one doesn't. Why? Which one is the "proper" way to do it?

推荐答案

您必须使用:

case item
when MyClass
...

我遇到了同样的问题: 如何在"case when"情况下捕获Errno :: ECONNRESET类?

I had the same problem: How to catch Errno::ECONNRESET class in "case when"?

这篇关于Ruby类类型和case语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆