From: Henry Maddocks Date: 2012-05-23T10:35:47+09:00 Subject: Re: Disginguishing object types On 23/05/2012, at 3:03 AM, "Jan E." wrote: > Hi, > > Jeremy W. wrote in post #1061681: >> >> Probably the easiest way is: >> http://ruby-doc.org/core-1.9.3/Object.html#method-i-kind_of-3F >> >> You might also want to consider: >> http://ruby-doc.org/core-1.9.3/Object.html#method-i-respond_to-3F > > To explain this a bit: In Ruby you usually don't judge objects by their > "type" (class) but by the existance of certain methods. This is called > duck typing. > > The idea behind this is that the *behaviour* of an object matters, not > so much the class. Also, where possible, don't check for an arbitrary method, check for a method you need or are about to call on the object in question. This will indicate more clearly why you care what type the objects are. > However, in your case it's probably best to explicitly check for the > Array class and the String class (or a subclass). Unfortunately String and Array share a lot of method names. Henry