开发者

How do you use max_by with an enumerable if you need to filter out values?

开发者 https://www.devze.com 2023-03-10 19:03 出处:网络
How would I do this?Right now it\'s creating nil values when user is male, and then the <==> operation is failing.

How would I do this? Right now it's creating nil values when user is male, and then the <==> operation is failing.

@user.max_by{|user_id, u开发者_高级运维ser| user.height if user.female?}


You can chain these together, so do your selection before your aggregation

@user.select{|user| user.female?}.max_by{|user_id, user| user.height}

Also, you should be able to simplify (just a little syntax sugar):

@user.select(&:female?).max_by(&:height)
0

精彩评论

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