I know little about ruby, my following implementation works, but I really feel the approach curbsome
@surfaces = []
Surface.all.each do |surface|
@surfaces << surface.name
end
the purpose is to extract name value of each surface objects and form them into an array, can anyone tell me the smart way to do this? thanks
Use
Enumerable#mapto turn one enumerable into another enumerable with a 1-to-1 mapping of elements. Combine withSymbol#to_procwhen the mapping involves simply invoking a single method on each element of the input enumerable.Both of these are equivalent: