2013-03-21 76 views
0

我的陣列是拒絕來自數組元素的有效方式

arr = ["wow what", "what anice", "anice day.currently", "day.currently i", "i am", "am in", "in delhi", "delhi but", "but in", "in night", "night i", "i am", "am going", "going to", "to us"] 
    arr.each do |el| 
    if !el.match('in') && !el.match('is').blank? 
     fresh_arr << el 
    end 

,但我有110K元素的數組,並給它8SEC的是,太多的時間我可以做到這一點任何其他方式

THX

+1

http://www.ruby-doc.org/core-2.0/Array.html#method-i-delete_if – demas 2013-03-21 09:29:32

+0

'字符串的所有元素#include?'是'String#match'的2-10倍。 – 2013-03-21 12:29:06

回答

3

使用delete_if

arr.delete_if do |e| 
    e.match('in') && e.match('is').blank? 
end 
arr 
+2

reject_if不是數組方法。 http://www.ruby-doc.org/core-2.0/Array.html – Arjan 2013-03-21 09:35:22

+0

我想你的意思是delete_if – Arjan 2013-03-21 09:36:58

+0

謝謝Arjan,你是對的。 – 2013-03-21 19:22:09

2

試試這個

arr.reject { |i| i.match('in') || i.match('is').blank? } 
0

您可以選擇您需要通過執行此

arr.select{|el| !el.match('in') && !el.match('is').blank?}