From: Walton Hoops Date: 2010-01-21T06:54:22+09:00 Subject: Re: Looking for object.inherits?(Classname) On 1/20/2010 2:36 PM, Walton Hoops wrote: > On 1/20/2010 12:05 PM, Xeno Campanoli wrote: >> Robert Klemme wrote: >>> 2010/1/20 Xeno Campanoli : >>>> I'm looking for a way to make sure an object being passed is in >>>> some class >>>> family, or to say it otherwise, inherits some specific class, but not >>>> necessarily directly. In fact, it could be a >>>> great-great-grandparent class, >>>> and that would be okay, and needs to be okay. Is there something >>>> where I >>>> can take an object and go >>>> >>>> unless object.inherits?(MyClass) >>>> raise SyntaxError, "etc." >>>> end >>> >>> First of all that would not be a SyntaxError because the syntax is not >>> affected. Using this error type will create misleading error >>> messages. >> >> If I want a method that only takes objects of family Xclass, and I >> get one passed that is not in that family, isn't that a syntax >> error? Bad type passed in that argument place? What's the >> difference between that and passing a string when it needs say an array? > > Nope, that's an ArgumentError. Example: > irb(main):001:0> Integer("boo") > ArgumentError: invalid value for Integer: "boo" > from (irb):2 > irb(main):002:0> > > Integer("boo") syntactically correct, yet "boo" is not a valid > argument for this function. > Whoops, my bad, actually the situation you describe is a TypeError: irb(main):002:0> Integer([1,2,3]) TypeError: can't convert Array into Integer from (irb):3 irb(main):003:0> again, still syntactically sound, but Integer can't take an array.