[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Determining lists,strings and numbers(Newbie question

vimal

6/28/2008 6:57:00 AM

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
3 Answers

Peña, Botp

6/28/2008 7:47:00 AM

0

From: vimal [mailto:cool.vimalsmail@gmail.com]=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

vimal

6/28/2008 7:59:00 AM

0

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

Jim Cochrane

6/28/2008 7:04:00 PM

0

On 2008-06-28, vimal <cool.vimalsmail@gmail.com> 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?

--