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
出于某种原因,其中第一个有时有效,第二个无效,而有时,第二个有效而第一个无效。为什么?哪一种是“正确”的做法?
What is the difference between
case item.class
when MyClass
# do something here
when Array
# do something different here
when String
# do a third thing
end
and
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
你必须使用:
我遇到了同样的问题:
如何在“case when”中捕获 Errno::ECONNRESET 类?
You must use:
I had the same problem:
How to catch Errno::ECONNRESET class in "case when"?
是的,Nakilon 是正确的,您必须知道 Threequal === 运算符如何作用于
when
子句中给出的对象。在 Ruby 中,真正
理解的情况是调用一个 Threequal 方法(例如
MyClass.===(item)
),并且可以定义该方法来执行您想要的任何操作,然后您可以使用精确的案例陈述Yeah, Nakilon is correct, you must know how the threequal === operator works on the object given in the
when
clause. In Rubyis really
Understand that case is calling a threequal method (
MyClass.===(item)
for example), and that method can be defined to do whatever you want, and then you can use the case statement with precisionw您可以使用:
...当以下扭曲不可能时:
原因是
case
使用===
和===
运算符描述的关系是不可交换的。例如,5
是一个Integer
,但是Integer
是5
吗?这就是您应该如何看待case
/when
。You can use:
...when the following twist is not possible:
The reason for this is that
case
uses===
, and the relationship the===
operator describes is not commutative. For example,5
is anInteger
, but isInteger
a5
? This is how you should think ofcase
/when
.在 Ruby 中,类名是一个常量,它引用描述特定类的
Class
类型的对象。这意味着在 Ruby 中说MyClass
相当于在 Java 中说MyClass.class
。obj.class
是一个Class
类型的对象,描述obj
的类。如果obj.class
是MyClass
,则obj
是使用MyClass.new
创建的(粗略地说)。MyClass
是一个Class
类型的对象,它描述使用MyClass.new
创建的任何对象。MyClass.class
是MyClass
对象的类(它是Class
类型的对象的 class,描述使用MyClass.new
创建的任何对象)。换句话说,MyClass.class == Class
。In Ruby, a class name is a constant that refers to an object of type
Class
that describes a particular class. That means that sayingMyClass
in Ruby is equivalent to sayingMyClass.class
in Java.obj.class
is an object of typeClass
describing the class ofobj
. Ifobj.class
isMyClass
, thenobj
was created usingMyClass.new
(roughly speaking).MyClass
is an object of typeClass
that describes any object created usingMyClass.new
.MyClass.class
is the class of theMyClass
object (it's the class of the object of typeClass
that describes any object created usingMyClass.new
). In other words,MyClass.class == Class
.这取决于您的
item
变量的性质。如果它是一个对象的实例,例如那么
,但如果它本身是一个类,例如
,那么它将是一个
Class
对象,所以编辑:请参阅如何在“case when”中捕获 Errno::ECONNRESET 类?正如 Nakilon 所说,因为我的回答可能是错误的。
It depends on the nature of your
item
variable. If it is an instance of an object, e.g.then
but if it is a class in itself e.g
then it will be a
Class
object, soEDIT: please refer to How to catch Errno::ECONNRESET class in "case when"? as stated by Nakilon since my answer could be wrong.