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

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]

i do things like this alot too. in my experience it is easiest to do this
with an object rather than in a procedural way. i don’t know your exact
setup, but it sounds like you are probably tracking things by key and position
so you can maintain an ordered hash (otherwise you’d simply be hashing on int
or string). why not simply create a class to do the work? you can put all
the code in one place and even write a unit test for the class. something
like

class LookUpTable < Hash
def ary; @ary ||= ; end
def k; (Numeric === k ? ary : self)[k]; end
def = k,v; ary << super; end
end

lut = LookUpTable.new
keys = :a, :b, :c
keys.each{|k| lut[k] = k.to_s}

p lut
lut.size.times{|i| printf “lut[%d] => %s\n”, i, lut[i]}

END
{:b=>“b”, :a=>“a”, :c=>“c”}
lut[0] => a
lut[1] => b
lut[2] => c

this may not be your exact case but my point is simply this: if you are going
impliment a lookup based on key type, why not build a class that does a lookup
based on key type? this way the key checking can be done in one place.

it’s so easy to crank out classes in ruby that i do it anytime i’m associating
methods with data.

regards.

-a

···

On Tue, 18 Nov 2003, Raphael Bauduin wrote:

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).

ATTN: please update your address books with address below!

===============================================================================

EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
PHONE :: 303.497.6469
ADDRESS :: E/GC2 325 Broadway, Boulder, CO 80305-3328
STP :: Solar-Terrestrial Physics Data | NCEI
NGDC :: http://www.ngdc.noaa.gov/
NESDIS :: http://www.nesdis.noaa.gov/
NOAA :: http://www.noaa.gov/
US DOC :: http://www.commerce.gov/

The difference between art and science is that science is what we
understand well enough to explain to a computer.
Art is everything else.
– Donald Knuth, “Discover”

/bin/sh -c ‘for l in ruby perl;do $l -e “print "\x3a\x2d\x29\x0a"”;done’
===============================================================================