-1

我有两个实例值:@a 和@b,有类似哈希的....(将在伪示例中解释)例如在@ai 中有:

*Field1 | Field2 | Field3*

CONTITECH | CT942K1 | 123
CONTITECH | CT722K1 | 123
ATE | AB2123 | 123

在@bi 中有:

CONTITECH | CT942K1 | 456
BREMBO | TE132 | 123

如何做到这一点@c = @a - @b 将只包含不在@b 中的值?

例如 @c = @a - @b 将包含:

CONTITECH | CT722K1 | 123
ATE | AB2123 | 123

因此,只有当 2 个字段相同时,我必须从 @a 中删除来自 @b 的相同值...

如果示例不清楚,请写在评论中....我需要的只是从@a 中删除所有相同的@b 中的(field1 和field2)条目...如何做到这一点?

如果这有帮助:代码:这是@b(@articles):

@articles = Article.find(:all, :conditions => ["ART_ID in (?) ", @search.map(&:ARL_ART_ID)])

和@a(@non_original):

@non_original = []
    nr_condition = "*" + art_nr.to_s.gsub(/[^0-9A-Za-z]/, '').upcase + "*"
    if art_nr.length > 3
      art = search_not_oem(art_nr)
      @search = CrossList.find(:all, :conditions => ['MATCH (cross_value) AGAINST (? IN BOOLEAN MODE)', nr_condition])
      if @search.present?
        @prlist = PriceList.where("id IN (?)", @search.map(&:price_list_id))
        if  @prlist.present?
          @prlist.each do |p|
            #@all_supp = Supplier.all
            #if @all_supp.find{|item| item.SUP_BRAND.gsub(/[^0-9A-Za-z]/, '').include?(p.brand.gsub(/[^0-9A-Za-z]/, '').upcase)} && art.present?
            #  logger.warn("!!!!!! if")
            #else
              @non_original << p
            #end                  
          end
        end
      end
    end
@non_original

我需要的是@res = @non_original - @articles,确保它们具有不同的字段,但具有相同的数据......

4

1 回答 1

0

试试这个:

@res = @non_original.reject do |obj|
  @articles.any? {|art| art.field1 == obj.field1 && art.field2 == obj.field2 }
end

That should put into @res all objects from @non_original, except for those whose field1 and field2 match those of any element in @articles. The objects in @res will be the same type as @non_original - that is, PriceList.

于 2013-01-31T14:45:06.443 回答