简体   繁体   English

Ruby:不区分大小写的数组比较

[英]Ruby: Case-Insensitive Array Comparison

刚发现这个比较实际上是区分大小写的。任何人都知道一种不区分大小写的方法来完成同样的比较吗?

CardReferral.all.map(&:email) - CardSignup.all.map(&: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 : 否则,您必须使用find_all手动进行比较或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. 我建议阅读Ruby的标准类型的参考可能会帮助你提出这样的代码。 For example, "Programming Ruby 1.9" has all methods of the Enumerable object explained starting on page 487 ( find_all is on page 489). 例如,“Programming Ruby 1.9”具有从第487页开始解释的Enumerable对象的所有方法( find_all在第489页)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM