如何检查变量是否是类的实例?

时间:2023-01-01 16:28:09

In Java, you can do instanceof. Is there a Ruby equivalent?

在Java中,可以使用instanceof。有一个Ruby版本吗?

4 个解决方案

#1


111  

It's almost exactly the same. You can use Object's instance_of? method:

几乎是一样的。可以使用对象的instance_of?方法:

"a".instance_of? String # => true
"a".instance_of? Object # => false

Ruby also has the is_a? and kind_of? methods (these 2 are aliases, and work exactly the same), which returns true is one of the superclasses matches:

Ruby也有is_a吗?和kind_of吗?方法(这两个是别名,工作方式完全相同),返回true的是超类匹配之一:

"a".is_a? String # => true
"a".is_a? Object # => true

#2


8  

kind_of? and is_a? are synonymous. They are Ruby's equivalent to Java's instanceof.

kind_of吗?和is_a吗?是同义的。它们相当于Ruby的Java实例。

instance_of? is different in that it only returns true if the object is an instance of that exact class, not a subclass.

instance_of吗?不同的是,它只返回true,如果对象是那个确切类的实例,而不是子类。

#3


6  

Have look at instance_of? and kind_of? methods. Here's the doc link http://ruby-doc.org/core/classes/Object.html#M000372

看看instance_of吗?和kind_of吗?方法。这里是doc链接http://ruby-doc.org/core/classes/Object.html#M000372

#4


5  

I've had success with klass, which returns the class object. This seems to be Rails-specific.

我已经成功地使用了klass,它返回类对象。这似乎是铁路特有的。

Sample usage:

示例用法:

class Foo
end

Foo.new.klass
# => Foo

Foo.new.klass == Foo
# => true

Foo.new.klass == "Foo"
# => false

There is also a method that accomplishes this: Object.is_a?, which takes the class object as an argument and returns true if self is an instance of the class or an instance of a subclass.

还有一个方法可以实现这一点:objec .is_a?,将类对象作为参数,如果self是类的实例或子类的实例,则返回true。

#1


111  

It's almost exactly the same. You can use Object's instance_of? method:

几乎是一样的。可以使用对象的instance_of?方法:

"a".instance_of? String # => true
"a".instance_of? Object # => false

Ruby also has the is_a? and kind_of? methods (these 2 are aliases, and work exactly the same), which returns true is one of the superclasses matches:

Ruby也有is_a吗?和kind_of吗?方法(这两个是别名,工作方式完全相同),返回true的是超类匹配之一:

"a".is_a? String # => true
"a".is_a? Object # => true

#2


8  

kind_of? and is_a? are synonymous. They are Ruby's equivalent to Java's instanceof.

kind_of吗?和is_a吗?是同义的。它们相当于Ruby的Java实例。

instance_of? is different in that it only returns true if the object is an instance of that exact class, not a subclass.

instance_of吗?不同的是,它只返回true,如果对象是那个确切类的实例,而不是子类。

#3


6  

Have look at instance_of? and kind_of? methods. Here's the doc link http://ruby-doc.org/core/classes/Object.html#M000372

看看instance_of吗?和kind_of吗?方法。这里是doc链接http://ruby-doc.org/core/classes/Object.html#M000372

#4


5  

I've had success with klass, which returns the class object. This seems to be Rails-specific.

我已经成功地使用了klass,它返回类对象。这似乎是铁路特有的。

Sample usage:

示例用法:

class Foo
end

Foo.new.klass
# => Foo

Foo.new.klass == Foo
# => true

Foo.new.klass == "Foo"
# => false

There is also a method that accomplishes this: Object.is_a?, which takes the class object as an argument and returns true if self is an instance of the class or an instance of a subclass.

还有一个方法可以实现这一点:objec .is_a?,将类对象作为参数,如果self是类的实例或子类的实例,则返回true。