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

In Smalltalk, is there a way to search for all available methods (any object), for example, containing a word convert(case-insensitive search), as well as a word string? (method name, not source code)

+4
source share
3 answers

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

Pharo

Go through all the classes, and then from each class select all the methods that suit 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 does not have a good API, but it can also be done.

(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

( Pharo Squeak, ifNotNil: GNU Smalltalk)

VW #flatten, . #findSameAs:startingAt:wildcard: .

(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 ]

, , , . .

+6

, ( /)

SystemNavigation default browseAllSelect:[:e |
(e selector includesSubstring:'convert' caseSensitive:false)
    and:[e selector includesSubstring:'string' caseSensitive:false]]
+2

, @Peter.

, (, Dolphin) #withAllSubclasses , . - @Peter .

,

selectors := OrderedCollection new.
Object withAllSubclasses do: [:class | | matching |
  matching := class selectors select: [:s |
    (s includesString: 'onvert') and: [s includesString: 'tring']].
  selectors addAll: matching.
  matching := class class selectors select: [:s |
    (s includesString: 'onvert') and: [s includesString: 'tring']].
  selectors addAll: matching].
^selectors

, 'convert' 'string', .

Another difference with my code is that it iterates over the selectorsclass, not its methods.

UPDATE

Please note that I used two enumerations because we cannot do this:

class selectors , class class selectors select: [:s |

etc .. The reason is that selectorsa classcomes in Setand they don’t understand #,.

We could do this:

all := class selectors addAll: class class selectors; yourself.
all selectors select: [:s |

etc .. (pay attention to use #yourself)

+1
source

Source: https://habr.com/ru/post/1621006/


All Articles