# How to find out object type

**URL:** <https://www.ruby-forum.com/t/how-to-find-out-object-type/86134>\
**Category:** Ruby\
**Created:** [February 20, 2007, 5:38am UTC](https://www.ruby-forum.com/t/how-to-find-out-object-type/86134 "2007-02-20T05:38:31Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Phy\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a587f6/32.png) [@Phy\_P](https://www.ruby-forum.com/u/Phy_P)\
**Post date:** [February 20, 2007, 5:38am UTC](https://www.ruby-forum.com/t/how-to-find-out-object-type/86134/1 "2007-02-20T05:38:31Z")

</div>

Hello,

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

TIA,  
Phy

---

<div class="post-metadata">

**Author:** ![Phy\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a587f6/32.png) [@Phy\_P](https://www.ruby-forum.com/u/Phy_P)\
**Post date:** [February 20, 2007, 5:43am UTC](https://www.ruby-forum.com/t/how-to-find-out-object-type/86134/2 "2007-02-20T05:43:31Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Phy\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a587f6/32.png) [@Phy\_P](https://www.ruby-forum.com/u/Phy_P)\
**Post date:** [February 20, 2007, 5:51am UTC](https://www.ruby-forum.com/t/how-to-find-out-object-type/86134/3 "2007-02-20T05:51:31Z")

</div>

On Feb 19, 2007, at 11:38 PM, Phy P. 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].class.new # 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  
self[-2]  
end  
b.penultimate # 20

Gary W.
