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)
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.
Object withAllSubclasses map [allMethods] inject [concatArray] contain 'keyword' contain 'keyword2'
(this is just pseudocode) –
Emir #collect:
is the smalltalk equivalent of map
, however I am not sure what you mean by [concatArray]
. What are you adding there? –
Munda [[1,3,5], [2,4,6], ["hello", "world"]].inject(:concat) => [1, 3, 5, 2, 4, 6, "hello", "world"]
Interesting... I think I can understand inject
better than before: it is like to inject an operator in between the elements: [1,3,5].inject(:+) => 9
and [1,3,5].inject(:-) => -7
–
Emir inject:into:
, that is equivalent to ruby's inject(initial) { |memo, obj| block }
. So you could use that to concat collections... e.g. #(#(1 3 5) #(2 4 6) #('hello' 'world')) inject: OrderedCollection new into: [ :injected :next | injected addAll: next; yourself ].
(it's bit more heavy handed, but normally you would flatten it with #(#(1 3 5) #(2 4 6) #('hello' 'world')) flattened
) –
Munda #withAllSubclasses
will visit only classes, not metaclasses. Therefore the methodDictionaries your code will select from will correspond to methods in the instance side of each class. –
Knotting Object
, which means also metaclasses (because in Smalltalk everything is Object). Try String class isKindOf: Object
or Object withAllSubclasses includes: String class
. –
Munda #withAllSubclasses
also includes the metaclasses (I didn't know that.) It looks like this is a dialect-dependent behavior. In some dialects (e.g., Dolphin) only classes (and not metaclasses) are collected by #withAllSubclasses
. –
Knotting :cls
does not work there –
Emir [ :cls | ... ]
is a regular BlockClosure
syntax supported by all mentioned dialects, including VW. –
Munda This may not work on all smalltalk dialects, but it works at least with squeak and pharo (other smalltalks may have similar tools/classes)
SystemNavigation default browseAllSelect:[:e |
(e selector includesSubstring:'convert' caseSensitive:false)
and:[e selector includesSubstring:'string' caseSensitive:false]]
This is more a complement to the answer given by @Peter.
Be aware that in some dialects (e.g., Dolphin) the message #withAllSubclasses
will only collect classes, and not metaclasses. Because of that the enumerations in @Peter's answer should add all metaclasses in an explicit way.
For instance,
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
Note BTW that I've removed the first letter from both 'convert'
and 'string'
to cheaply prevent case mismatches.
Another difference with my code is that it iterates over the selectors
of a class rather than over its methods.
UPDATE
Note that I've used two enumerations because we cannot do this:
class selectors , class class selectors select: [:s |
etc. The reason is that the selectors
of a class
come in a Set
and these do not understand #,
.
We could have done instead:
all := class selectors addAll: class class selectors; yourself.
all selectors select: [:s |
etc. (note the use of #yourself
)
Object withAllSubclasses, Class withAllSubclasses
work also? (So then the matching would be done on a single collection) –
Munda Class
has no subclasses. It has no instances either! The only reason for Class
to exist is as a mean to put behavior common to all classes (but not to metaclasses) –
Knotting This is to add different flavor to the list above.
@Smalltalk/X
Object withAllSubclasses flatCollect: [ :cls |
cls methodDictionary values select: [ :method |
(method selector includesSubstring: 'convert' caseSensitive: false) and: [
(method selector includesSubstring: 'string' caseSensitive: false) ]
]
].
© 2022 - 2024 — McMap. All rights reserved.