有條件爲真時從數組中彈出項目並返回集合的Ruby成語嗎?有條件成立時從數組中彈出項目的紅寶石成語
即,
# Would pop all negative numbers from the end of 'array' and place them into 'result'.
result = array.pop {|i| i < 0}
從我可以告訴,像上面不存在。
我目前使用
result = []
while array.last < 0 do
result << array.pop
end
更通用;彈出數組末尾的所有匹配項。如果遇到不匹配的項目,請停止。 – 2013-03-14 16:50:20
「不要跳過不匹配」:我無法解析此問題。 – 2013-03-14 16:51:24
我已經改寫過它 – 2013-03-14 16:52:46