开发者

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

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜