From: Raphael Bauduin Date: 2003-11-19T01:12:22+09:00 Subject: Re: testing argument type and duck typing, newbie question T. Onoma wrote: > 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 I use them as a key to extract an element from a Hash (if arg is a string) or from a Array (if arg is an integer). I'll use is_a?String and is_a?Numeric as Gabriele wrote. Your little explanation and example helped me understand what ducktyping is good for. Thanks! Raph [zip]