I'm trying to reactor this code. Any advice?
@contest_users = []
@contest.squads.each do |squad|
@contest_users << squad.users[0]
end
使用map
而不是each
:
@contest_users = @contest.squads.map { |squad| squad.users.first }
@contest_users = @contest.squads.each_with_object([]) do |squad, array|
array << squad.users[0]
end
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.