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

如何查找Smalltalk中可用的所有方法并按名称搜索?,smalltalk,Smalltalk,在Smalltalk中,是否有一种方法可以搜索任何对象的所有可用方法,例如,包含单词convert不区分大小写的搜索,并且还包含单词string?方法名称,而不是源代码在Smalltalk中,您可以直接访问所有类、它们的方法和源代码,因此您可以浏览它们 福络 检查所有类,然后从每个类中选择符合您需要的所有方法或使用Finder工具 Object withAllSubclasses flatCollect: [ :cls | cls methods select: [ :method |

在Smalltalk中,是否有一种方法可以搜索任何对象的所有可用方法,例如,包含单词convert不区分大小写的搜索,并且还包含单词string?方法名称,而不是源代码在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
视觉作品 还有Pharo和Squeak,还有ifNotNil:GNU Smalltalk

大众没有扁平化,所以它是显式实现的。对于不区分大小写的搜索,还可以使用sameas: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似乎有不同的对象模型,见下文。

这是对@Peter给出的答案的补充

请注意,在某些方言中,例如Dolphin,带有所有子类的消息只收集类,而不收集元类。正因为如此,@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”中删除了第一个字母,以廉价地防止大小写不匹配

我的代码的另一个区别是,它迭代类的选择器,而不是它的方法

更新

请注意,我使用了两个枚举,因为我们不能这样做:

class selectors , class class selectors select: [:s |
原因是类的选择器是一组的,它们不理解

我们本可以这样做:

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

等等。注意你自己的用法。这可能不适用于所有的smalltalk方言,但至少它是有效的 对于squeak和pharo,其他Smalltalk可能有类似的工具/类

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

你能告诉我们你用的是哪种方言吗。。。最好是一种更标准的方法。。。否则GNU Smalltalk、Dolphin、Squeak或PharoBy包含在方法的源代码中,或者方法的名称中?方法的名称。。。除了这个问题之外,还可以通过像Object WithAllSubclass map[allMethods]inject[concatArray]包含'keyword'包含'keyword2'这样的方法来实现这一点。这只是pseudocodecollect:smalltalk等同于map,但是我不确定你所说的[concatArray]是什么意思。你在那里添加什么?获取所有数组,将它们相加形成一个大数组。。。是注入式的还是仅仅使用某种形式的连接我在答案中添加了替代代码,这就是你的想法吗?总是把它作为一个集合,然后对它应用过滤器?所以我明白了。。。有点想在一行中完成。我所说的注入就像Ruby中的[[1,3,5],[2,4,6],[hello,world]]一样。。。我想我能比以前更好地理解inject:它就像在元素[1,3,5]之间插入一个操作符。inject:+=>9和[1,3,5]。inject:-=>-7是否会对所有子类进行对象化,对所有子类进行类化也会起作用?因此,匹配将在单个计算机上进行collection@Peter恐怕没有。这个类没有子类。它也没有实例!类存在的唯一原因是将行为放在所有类而不是元类的公共位置