Forum: Ruby How to find out object type

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
555c1193071e97926fa67c038244c047?d=identicon&s=25 Phy Prabab (Guest)
on 2007-02-20 05:38
(Received via mailing list)

I am curious, is there a way to find out an object type?

9d1f5d2d9de70bd9a934f557dc95a406?d=identicon&s=25 Daniel ----- (liquid)
on 2007-02-20 05:43
(Received via mailing list)
object.kind_of?( Array )  #=>  To test if it is something
object.is_a?( Array ) #=> Same

object.class #=> Tells you what class the object thinks it is.
E7559e558ececa67c40f452483b9ac8c?d=identicon&s=25 Gary Wright (Guest)
on 2007-02-20 05:51
(Received via mailing list)
On Feb 19, 2007, at 11:38 PM, Phy Prabab wrote:
> I am curious, is there a way to find out an object type?

You can find out what class gave 'birth' to the object via #class:

[1,2].class   # Array
3.class       # Fixnum

Note that #class does *not* return a string (e.g., "Array") but instead
returns a reference to the actual class object:

a = [1,2]        # allocates a brand new array
p a           # []

It is important to realize that the 'class' of an object is really just
a hint at how an object will respond to methods.  It might be a really
good hint, but a hint nevertheless since objects are free to override
method definitions provided by the class or even to add methods that
the class doesn't provide:

b = [10,20,30]
def b.penultimate
b.penultimate   #  20

Gary Wright
This topic is locked and can not be replied to.