Determining lists,strings and numbers(Newbie question)

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

From: vimal [mailto:[email protected]]

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

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

On 2008-06-28, vimal [email protected] 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?