0

我有以下 7 行代码,我怎样才能让它们更短更简洁?

max_group_size = 0
wrong_services.each do |service|
  group_size = service.iep_service.group_size
  if group_size > max_group_size then
    max_group_size = group_size
  end
end
4

2 回答 2

5
max_group_size = wrong_services.map {|service| service.iep_service.group_size }.max
于 2012-09-24T19:58:56.927 回答
2
max_group_size = wrong_services.max_by{|service| service.iep_service.group_size}.iep_service.group_size

(评论后编辑)

于 2012-09-24T20:07:33.273 回答