Forum: Ruby Type of class

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
J. m. (Guest)
on 2007-02-25 20:27
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 :
     # 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
Stefano C. (Guest)
on 2007-02-25 21:24
(Received via mailing list)
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
J. m. (Guest)
on 2007-02-26 01:17
Stefano C. 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.
Clifford H. (Guest)
on 2007-02-26 03:20
(Received via mailing list)
J. mp wrote:
> a.instance_of? Array

and kind_of?, and (Array === a)... check them all out
and learn what they do.
This topic is locked and can not be replied to.