From: Daniel Schierbeck Date: 2006-05-12T19:14:58+09:00 Subject: Re: Newb question - object type tests? Adam Bloom wrote: > Hello all, > > I feel like I definitely should know this, but how do you test the type > of an object? For example: > > item = ["hello"] > item.array? >> true > item = "hello" > item.array? >> false > > extending that, how do I test the type of a class that I made? Others have already shown you how to test the *class* of an object (note that in Ruby, the class isn't the same as the type). If you want to be more Rubyish, try this: item = ["hello"] item.respond_to? :to_ary => true item = "hello" item.respond_to? :to_str => true So if you're writing a method that requires a string, just do this: def foo(bar) str = bar.to_str str.split(... end That way, all classes that consider themselves strings need only define a #to_str method. If they actually do define the same methods as String, #to_str can just return `self'; otherwise it can return a string representation. Cheers, Daniel