-1

I have two instance values: @a, and @b, there are hash-like.... (will explain on pseudo examples) For example in @a i have:

*Field1 | Field2 | Field3*

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

in @b i have:

CONTITECH | CT942K1 | 456
BREMBO | TE132 | 123

How to do that @c = @a - @b will contain only values which are not in @b?

For example @c = @a - @b will contain:

CONTITECH | CT722K1 | 123
ATE | AB2123 | 123

So only if 2 field are same, i must delete from @a same value from @b...

If don't clear by examples, write in comments.... What i need is just delete from @a all same by (field1 and field2) entries in @b... How to do this?

if this will help: code: this is @b (@articles):

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

and @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

what i need is to @res = @non_original - @articles, sure they have different fields, but with same data...

Valdis Azamaris
  • 1,433
  • 5
  • 22
  • 47

1 Answers1

0

Try this:

@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.

Chowlett
  • 45,935
  • 20
  • 116
  • 150