6

Is there a smalltalk message that will answer with a boolean value if a given method (example: passed with #aMethod) belongs to a given class (or its hierarchy)?

I want to say something like —

(self containsMethod:#aMethod) ifFalse:[...blah blah].

Obviously, containsMethod: is a placeholder for some message I hope exists. Oh, and self's superclass in this example is Object. Thanks!

Wes Field
  • 3,291
  • 6
  • 23
  • 26
  • 1
    For future reference, Squeak's [Method Finder](http://wiki.squeak.org/squeak/1916) has a fantastic search by example feature. The query `Object new. #asString. true.` finds `#respondsTo:` and some other messages. – tom May 06 '13 at 11:36

1 Answers1

14

You can use #respondsTo:

1 respondsTo: #+.

and there is the class-side counter part canUnderstand:

1 class canUnderstand: #+.
Integer canUnderstand: #+.
camillobruni
  • 2,298
  • 16
  • 26
  • 1
    Note the hierarchy-searching class-side equivalent is `#canUnderstand:` – Tobias May 06 '13 at 11:29
  • 1
    Is there something for just the class it self? without looking in the inheritance hierarchy? I need a method to answer whether a class has a certain method or not – TomerZ Dec 10 '14 at 22:27
  • 1
    Sure, you can do `Integer includesSelector: #asNumber` which will return `false` since `#asNumber` is only implemented in `Number`, a superclass of `Integer`. – camillobruni Dec 24 '14 at 15:34
  • @camillobruni Thanks for includesSelector: I was searching for it. – unom Jan 16 '17 at 15:15