# Type of class

**URL:** <https://rubytalk.org/t/type-of-class/35673>\
**Category:** ruby-talk\
**Created:** [25 February 2007 18:27 UTC](https://rubytalk.org/t/type-of-class/35673 "2007-02-25T18:27:20Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![J\_mp](https://avatars.discourse-cdn.com/v4/letter/j/e9bcb4/32.png) [@J\_mp](https://rubytalk.org/u/J_mp)\
**Post date:** [25 February 2007 18:27 UTC](https://rubytalk.org/t/type-of-class/35673/1 "2007-02-25T18:27:20Z")

</div>

Hi  
How do I know if a variable is an instace of a given class

I have a method that returns a String or an Array and I have to know  
outside if the returned object is and String or an Array.

Is beacuse this :  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# File vendor/rails/activerecord/lib/active\_record/validations.rb,  
line 100  
100: def on(attribute)  
101: errors = @errors[attribute.to\_s]  
102: return nil if errors.nil?  
103: errors.size == 1 ? errors.first : errors  
104: end

How do I know that call on method on returned and array or an string?

Thanks

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Stefano\_Crocco](https://avatars.discourse-cdn.com/v4/letter/s/5f9b8f/32.png) [@Stefano\_Crocco](https://rubytalk.org/u/Stefano_Crocco)\
**Post date:** [25 February 2007 19:23 UTC](https://rubytalk.org/t/type-of-class/35673/2 "2007-02-25T19:23:55Z")

</div>

Alle domenica 25 febbraio 2007, J. mp ha scritto:

> Hi  
> How do I know if a variable is an instace of a given class
> 
> I have a method that returns a String or an Array and I have to now  
> outside if the returned object is and String or an Array.
> 
> Is beacuse this :  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# File vendor/rails/activerecord/lib/active\_record/validations.rb,  
> line 100  
> 100: def on(attribute)  
> 101: errors = @errors[attribute.to\_s]  
> 102: return nil if errors.nil?  
> 103: errors.size == 1 ? errors.first : errors  
> 104: end
> 
> How do I know that call on method on returned and array or an string?
> 
> Thanks

The is\_a? method tells whether the receiver is an instance of the class passed  
as argument or of one of its derived class:

a=[1,2,3]

a.is\_a?(Array)  
=\> true  
a.is\_a?(String)  
=\> false

If you need to know whether an object is an instance of exactly a specific  
class (not of derived class), you can use the == on the class:

class A  
end

class B \< A  
end

b=B.new  
b.is\_a?(A)  
=\> true  
b.class==A  
=\> false  
b.class==B  
=\> true

I hope this helps

Stefano

---

<div class="post-metadata">

**Author:** ![J\_mp](https://avatars.discourse-cdn.com/v4/letter/j/e9bcb4/32.png) [@J\_mp](https://rubytalk.org/u/J_mp)\
**Post date:** [25 February 2007 23:17 UTC](https://rubytalk.org/t/type-of-class/35673/3 "2007-02-25T23:17:19Z")

</div>

Stefano Crocco wrote:

> Alle domenica 25 febbraio 2007, J. mp ha scritto:
> 
> > 101: errors = @errors[attribute.to\_s]  
> > 102: return nil if errors.nil?  
> > 103: errors.size == 1 ? errors.first : errors  
> > 104: end
> > 
> > How do I know that call on method on returned and array or an string?
> > 
> > Thanks
> 
> The is\_a? method tells whether the receiver is an instance of the class  
> passed  
> as argument or of one of its derived class:
> 
> a=[1,2,3]
> 
> a.is\_a?(Array)  
> =\> true  
> a.is\_a?(String)  
> =\> false
> 
> If you need to know whether an object is an instance of exactly a  
> specific  
> class (not of derived class), you can use the == on the class:
> 
> class A  
> end
> 
> class B \< A  
> end
> 
> b=B.new  
> b.is\_a?(A)  
> =\> true  
> b.class==A  
> =\> false  
> b.class==B  
> =\> true
> 
> I hope this helps
> 
> Stefano

Thanks Stefano,  
Meanwhile I found (before seeing your response) another way, don't know  
which one is the best, but also works

a.instance\_of? Array

Maybe they are the same methods but one is alias of the other? Or  
there's any really difference between them?

Thanks again.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Clifford\_Heath3](https://avatars.discourse-cdn.com/v4/letter/c/ec9cab/32.png) [@Clifford\_Heath3](https://rubytalk.org/u/Clifford_Heath3)\
**Post date:** [26 February 2007 01:20 UTC](https://rubytalk.org/t/type-of-class/35673/4 "2007-02-26T01:20:06Z")

</div>

J. mp wrote:

> a.instance\_of? Array

and kind\_of?, and (Array === a)... check them all out  
and learn what they do.
