Multiply each item in an array by each item in another array

  • A+
Category:Languages

In Ruby, how can one multiply every element in one array by every element in another array, such that:

a = [1,2,3]  b = [4,5,6]  c = a*b = [4,5,6,8,10,12,12,15,18] 

 


This is not beautiful but returns what you want.

a.map{|aa| b.map{|bb| bb * aa}}.flatten

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: