Determining lists,strings and numbers(Newbie question)

V

vimal

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
 
P

Peña, Botp

From: vimal [mailto:[email protected]]=20
# 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) =3D> true or false
obj.kind_of?(class) =3D> 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 =3D B.new
b.instance_of? A #=3D> false
b.instance_of? B #=3D> true
b.instance_of? C #=3D> false
b.instance_of? M #=3D> false
b.kind_of? A #=3D> true
b.kind_of? B #=3D> true
b.kind_of? C #=3D> false
b.kind_of? M #=3D> true


irb(main):001:0> "asdf".is_a? String
=3D> true
irb(main):002:0> "asdf".is_a? Integer
=3D> false
irb(main):003:0> 1.is_a? Integer
=3D> true
irb(main):004:0> 1.is_a? Array
=3D> false
irb(main):005:0> ["",1].is_a? Array
=3D> true
 
V

vimal

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
 
J

Jim Cochrane

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?

--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top