Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find all methods available in Smalltalk and search by name?

Tags:

smalltalk

In Smalltalk, is there a way to search for all methods available (of any object), say, that contain the word convert (case insensitive search), and also contain the word string? (the method name, not the source code)

like image 485
nonopolarity Avatar asked Dec 18 '22 21:12

nonopolarity


1 Answers

In Smalltalk you have direct access to all classes, their methods and their source code, so you can go through them.

Pharo

Go over all the classes and then from each class select all methods that match your needs (or use the Finder tool).

Object withAllSubclasses flatCollect: [ :cls |
    cls methods select: [ :method |
        (method selector includesSubstring: 'convert' caseSensitive: false) and: [
        (method selector includesSubstring: 'string' caseSensitive: false) ]
    ]
].

GNU Smalltalk

GST doesn't have as nice API, but it can be done also.

(Object withAllSubclasses collect: [ :cls |
    cls methodDictionary ifNotNil: [ :dict |
        dict values select: [ :method |
            (method selector asLowercase indexOfSubCollection: 'convert' asLowercase) > 0 and: [
            (method selector asLowercase indexOfSubCollection: 'string' asLowercase) > 0 ]
        ]
    ]
]) join

VisualWorks

(also Pharo and Squeak, and with ifNotNil: also GNU Smalltalk)

VW doesn't have #flatten, so it's implemented explicitly. For case-insensitive search #findSameAs:startingAt:wildcard: can also be used.

(Object withAllSubclasses collect: [ :cls |
    cls methodDictionary values select: [ :method |
        (method selector asLowercase findString: 'convert' asLowercase startingAt: 1) > 0 and: [
        (method selector asLowercase findString: 'string' asLowercase startingAt: 1) > 0 ]
    ]
]) inject: #() into: [ :arr :each | arr, each ]

Dolphin

Dolphin seems to have different object model, see Leandro's answer below.

like image 181
Peter Uhnak Avatar answered Jun 05 '23 11:06

Peter Uhnak