Hi guys, I have a question. How when I perform an iteration via the map command for example, run as well a command ?
Example:
currentDependencies = softwares.map { |entry| entry.toSoftwareDependency}
Basically, everytime the map command is doing the iteration, I would too to execute the command: playCalculationAnimation.
I did something like this, but it don’t work, basically it kill the result of the map:
currentDependencies = softwares.map { |entry| entry.toSoftwareDependency && playCalculationAnimation}
If I remember it will return an empty array
Barney
2
#map
takes a block, and the { }
syntax is just a one-liner alternative for do - end
. (Though technically it can replace do-end
everywhere)
In your example:
currentDependencies = softwares.map do |entry|
playCalculationAnimation
entry.toSoftwareDependency
end
Or alternatively, if the order of execution matters:
currentDependencies = softwares.map do |entry|
software_dependency = entry.toSoftwareDependency
playCalculationAnimation
software_dependency
end
to make sure that the block returns the value that you want.
Unrelated, I recommend following the official coding style, because your current one looks a bit Java-like 
1 Like
This is a good use case for #tap
:
currentDependencies = softwares.map do |entry|
entry.toSoftwareDependency.tap { playCalculationAnimation }
end
2 Likes
Thank you so much ! It work with the method .any? too ?
1 Like