I have a simple question about rails syntax:
How can I find out to which class a object belongs?
I try to do someting like:
if class(object) == MyClass
Thanks Maechi
object.is_a?(MyClass)
object.kind_of?(MyClass)
Returns true if class is the class of obj, or if class is one of the superclasses of obj or modules included in obj. Aliased as ``
object.instance_of?(MyClass)
object.class == MyClass
Returns true if obj is an instance of the given class.
MyClass === object
Identical to is_a?
method. Useful for case statements like
case object
when MyClass
when OtherClass
…
end
You can do
if object.class == MyClass
or
if object.is_a?(MyClass)
The latter also returns true if object
is an instance of a subclass of MyClass
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With