Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby: Case-Insensitive Array Comparison

Just found out that this comparison is actually case-sensitive..Anyone know a case-insensitive way of accomplishing the same comparison?

CardReferral.all.map(&:email) - CardSignup.all.map(&:email)
like image 938
Trip Avatar asked Jan 21 '23 23:01

Trip


1 Answers

I don't think there is any "direct" way like the minus operator, but if you don't mind getting all your results in lowercase, you can do this:

CardReferral.all.map(&:email).map(&:downcase) - CardSignup.all.map(&:email).map(&:downcase)

Otherwise you'll have to manually do the comparison using find_all or reject:

signups = CardSignup.all.map(&:email).map(&:downcase)
referrals = CardReferral.all.map(&:email).reject { |e| signups.include?(e.downcase) }

I'd suggest that reading a reference of Ruby's standard types might help you come up with code like this. For example, "Programming Ruby 1.9" has all methods of the Enumerable object explained starting on page 487 (find_all is on page 489).

like image 76
Jo Liss Avatar answered Jan 30 '23 19:01

Jo Liss