A recent Rubocop update introduced a new cop “Style/MapIntoArray” which discourages developers from looping over a collection only to populate another collection with some output.
Instead of this approach we should be using array functions like #map
, #select
, #reject
etc.
# ❌ Violates Rubocop v1.63.0 Style/MapIntoArray
collection_one = []
collection_two.each do |item|
collection_one.push(item + 1)
end
# ✅ Complies with Style/MapIntoArray cop
collection_one = collection_two.map do |item|
item + 1
end