Ruby Select和Reject在一个方法中

是否有任何内置方法将select的function(找到块等于true的所有内容)和reject(找到块等于false的所有内容)组合在一起?

就像是

good, bad = list.magic_method { |obj| obj.good? } 

NIH和其他所有这些都不会太难。 🙂

看起来好像Enumerable.partition正是您所追求的。

 = Enumerable.partition (from ruby core) ------------------------------------------------------------------------------ enum.partition {| obj | block } -> [ true_array, false_array ] enum.partition -> an_enumerator ------------------------------------------------------------------------------ Returns two arrays, the first containing the elements of enum for which the block evaluates to true, the second containing the rest. If no block is given, an enumerator is returned instead. (1..6).partition {|i| (i&1).zero?} #=> [[2, 4, 6], [1, 3, 5]] 

有意思,我不知道那里有。 ri是一个了不起的工具……