是否有任何内置方法可以结合以下功能Enumerable.select
(找到该块等于 true 的所有内容)和Enumerable.reject
(找到该块中所有等于 false 的内容)?
就像是
good, bad = list.magic_method { |obj| obj.good? }
看起来好像Enumerable.partition http://www.ruby-doc.org/core/classes/Enumerable.html#M001496正是您所追求的。
= 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
是一个了不起的工具...
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)