gpt4 book ai didi

smalltalk - 如何查找 Smalltalk 中可用的所有方法并按名称搜索?

转载 作者:行者123 更新时间:2023-12-02 11:26:27 25 4
gpt4 key购买 nike

在 Smalltalk 中,有没有一种方法可以搜索(任何对象的)所有可用方法,例如包含单词 convert (不区分大小写的搜索),并且还包含单词 字符串? (方法名称,不是源代码)

最佳答案

在 Smalltalk 中,您可以直接访问所有类、它们的方法及其源代码,因此您可以浏览它们。

法罗

浏览所有类,然后从每个类中选择符合您需求的所有方法(或使用 Finder 工具)。

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

GNU Smalltalk

GST 没有那么好的 API,但也可以做到。

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

海豚

Dolphin 似乎有不同的对象模型,请参阅 Leandro's answer下面。

关于smalltalk - 如何查找 Smalltalk 中可用的所有方法并按名称搜索?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34383792/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com