开发者

Ruby: Case-Insensitive Array Comparison

开发者 https://www.devze.com 2023-01-28 06:41 出处:网络
Just found out that this comparison is actually case-sensitive..Anyone know a case-insensitive way of accomplishing the same 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(&a开发者_JS百科mp;:email)


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

0

精彩评论

暂无评论...
验证码 换一张
取 消