Ruby 类类型和 case 语句

发布于 2024-09-27 03:55:40 字数 421 浏览 3 评论 0原文

之间的区别是什么

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

jJeQQOZ5 2024-10-04 03:55:40

你必须使用:

case item
when MyClass
...

我遇到了同样的问题:
如何在“case when”中捕获 Errno::ECONNRESET 类?

You must use:

case item
when MyClass
...

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

拥抱影子 2024-10-04 03:55:40

是的,Nakilon 是正确的,您必须知道 Threequal === 运算符如何作用于 when 子句中给出的对象。在 Ruby 中,

case item
when MyClass
...
when Array
...
when String
...

真正

if MyClass === item
...
elsif Array === item
...
elsif String === item
...

理解的情况是调用一个 Threequal 方法(例如 MyClass.===(item)),并且可以定义该方法来执行您想要的任何操作,然后您可以使用精确的案例陈述

Yeah, Nakilon is correct, you must know how the threequal === operator works on the object given in the when clause. In Ruby

case item
when MyClass
...
when Array
...
when String
...

is really

if MyClass === item
...
elsif Array === item
...
elsif String === item
...

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

末骤雨初歇 2024-10-04 03:55:40

您可以使用:

case item.class.to_s
    when 'MyClass'

...当以下扭曲不可能时:

case item
    when MyClass

原因是 case 使用 ====== 运算符描述的关系是不可交换的。例如,5 是一个 Integer,但是 Integer5 吗?这就是您应该如何看待case/when

You can use:

case item.class.to_s
    when 'MyClass'

...when the following twist is not possible:

case item
    when MyClass

The reason for this is that case uses ===, and the relationship the === operator describes is not commutative. For example, 5 is an Integer, but is Integer a 5? This is how you should think of case/when.

何时共饮酒 2024-10-04 03:55:40

在 Ruby 中,类名是一个常量,它引用描述特定类的 Class 类型的对象。这意味着在 Ruby 中说 MyClass 相当于在 Java 中说 MyClass.class

obj.class 是一个 Class 类型的对象,描述 obj 的类。如果 obj.classMyClass,则 obj 是使用 MyClass.new 创建的(粗略地说)。 MyClass 是一个 Class 类型的对象,它描述使用 MyClass.new 创建的任何对象。

MyClass.classMyClass 对象的类(它是 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 saying MyClass in Ruby is equivalent to saying MyClass.class in Java.

obj.class is an object of type Class describing the class of obj. If obj.class is MyClass, then obj was created using MyClass.new (roughly speaking). MyClass is an object of type Class that describes any object created using MyClass.new.

MyClass.class is the class of the MyClass object (it's the class of the object of type Class that describes any object created using MyClass.new). In other words, MyClass.class == Class.

风筝有风,海豚有海 2024-10-04 03:55:40

这取决于您的 item 变量的性质。如果它是一个对象的实例,例如

t = 5

那么

t.class == Fixnum

,但如果它本身是一个类,例如

t = Array

,那么它将是一个 Class 对象,所以

t.class == 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.

t = 5

then

t.class == Fixnum

but if it is a class in itself e.g

t = Array

then it will be a Class object, so

t.class == Class

EDIT: please refer to How to catch Errno::ECONNRESET class in "case when"? as stated by Nakilon since my answer could be wrong.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文