sja
sja

Reputation: 347

Check values between two arrays

Feel like the title summed it up pretty well. I have two large arrays, all containing ids.

One is the old_list and one is the current_list. What I would like to do is this:

This is set as a background job that updates every 4 hours. Thus I want to see if any new value have appeared, or been removed since I last checked.

Here is what I have currently, which is not complete:

    twitter.follower_ids("#{uid}").each do |f_id|
      # unless user already has follower id saved
      unless followers.map(&:follower_id).include?(f_id.to_s)
        followers.create do |follower|
          follower.follower_id = f_id
        end
      end
    end

Upvotes: 0

Views: 35

Answers (1)

Arup Rakshit
Arup Rakshit

Reputation: 118271

You need to do the below Set operation :

(old_list & current_list) | current_list

Example :

old_v = [1,2,43]
new_v = [1,11,21]
(old_v & new_v) | new_v # => [1, 11, 21]

Array#& and Array#|.

Upvotes: 3

Related Questions