From: "T. Onoma" Date: 2003-11-19T00:32:55+09:00 Subject: Re: testing argument type and duck typing, newbie question Hi Raph, > I have a method taking one argument that can be an integer or a string. > How do I test the argument to know if it's a string or an integer? All > the duck typing things I've read make me wonder what's the best way > to determine if the argument is an integer or a string..... > > Thanks for helping me clarifying this. You simply need to consider how your methos is using that argument. What does your method do with the integer in contract to what it does with the string? If it treats them uniformly, that is to say, by calling methods on them that they both respond to equally, then you do not have to treat each case in any special way. That's Duck Typing. For example, try passing an integer and then a string to this def myduck(x) puts x + 4 end Both types work, no errors, just different results. On the other hand, if you call methods on them that are unique to their class then you will have to ask them what kind they are. One way to do this is with the case statement. Note that an integer comes in a couple of different forms Fixnum and Bignum: def mycase(x) case x when Fixnum, Bignum p "#{x}: I am an integer. You can count on me!" when String p "#{x}: I am a string of letters to do your writ." end end Hope that's helpful, -t0