Here is my code that works but I'm looking for a solution thats even cleaner. Im trying to avoid having to initalize my array first.
users = []
employees_to_import.each do |employee|
users << User.new(employee.slice(:first_name, :last_name, :email))
end
Is there a method in ruby that I can call to do something like this?
users = employees_to_import.push_each do |employee|
User.new(employee.slice(:first_name, :last_name, :email))
end
Not sure if a method like this exists, I didn't see anything in the docs but I figured I would ask.
employees_to_import.each_with_object([]) { |employee, users| users << User.new(employee.slice(:first_name, :last_name, :email)) }. Heremap(as @AmitA has used) is preferable, buteach_with_objectis the method of choice in many other situations. Among other things (pertaining to scope),each_with_objectavoids the need to first define the object (users = []) and then return it after the block (users).