# Determining lists,strings and numbers(Newbie question)

**URL:** <https://www.ruby-forum.com/t/determining-lists-strings-and-numbers-newbie-question/141650>\
**Category:** Ruby\
**Created:** [June 28, 2008, 9:00am UTC](https://www.ruby-forum.com/t/determining-lists-strings-and-numbers-newbie-question/141650 "2008-06-28T09:00:28Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [June 28, 2008, 9:00am UTC](https://www.ruby-forum.com/t/determining-lists-strings-and-numbers-newbie-question/141650/1 "2008-06-28T09:00:28Z")

</div>

name = “vimal”

if name.respond\_to?(“each”)  
print “Its a list\n”  
else  
print “Its a string\n”

output:  
Its a list  
=\> nil

The Condition is True,but its a “string” right!!

My quetion is,

How can one determine whether a given object is a list or string or  
number

thanks,  
vimal

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [June 28, 2008, 9:49am UTC](https://www.ruby-forum.com/t/determining-lists-strings-and-numbers-newbie-question/141650/2 "2008-06-28T09:49:35Z")

</div>

From: vimal [[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)]

# How can one determine whether a given object is a list or string or

# number

## botp@botp-desktop:~$ qri object.is\_a? ----------------------------------------------------------- Object#is\_a? obj.is\_a?(class) =\> true or false obj.kind\_of?(class) =\> true or false

```
 Returns true if class is the class of obj, or if class is one of
 the superclasses of obj or modules included in obj.

    module M; end
    class A
      include M
    end
    class B < A; end
    class C < B; end
    b = B.new
    b.instance_of? A #=> false
    b.instance_of? B #=> true
    b.instance_of? C #=> false
    b.instance_of? M #=> false
    b.kind_of? A #=> true
    b.kind_of? B #=> true
    b.kind_of? C #=> false
    b.kind_of? M #=> true

```

irb(main):001:0\> “asdf”.is\_a? String  
=\> true  
irb(main):002:0\> “asdf”.is\_a? Integer  
=\> false  
irb(main):003:0\> 1.is\_a? Integer  
=\> true  
irb(main):004:0\> 1.is\_a? Array  
=\> false  
irb(main):005:0\> [“”,1].is\_a? Array  
=\> true

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [June 28, 2008, 10:00am UTC](https://www.ruby-forum.com/t/determining-lists-strings-and-numbers-newbie-question/141650/3 "2008-06-28T10:00:42Z")

</div>

Thanks for ur reply

It was really helpful for me

But this looks very complex for me(the class and module types)  
Anyway i will try to figure it out later with ur fine examples

regards,  
vimal

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [June 28, 2008, 9:05pm UTC](https://www.ruby-forum.com/t/determining-lists-strings-and-numbers-newbie-question/141650/4 "2008-06-28T21:05:44Z")

</div>

On 2008-06-28, vimal [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Thanks for ur reply
> 
> It was really helpful for me
> 
> But this looks very complex for me(the class and module types)  
> Anyway i will try to figure it out later with ur fine examples
> 
> regards,  
> vimal

What reply?
