WKC CCC wrote:
> HI,
>
> Does anyone know of a function that multiplies the contents of an array.
> For example:
>
> one = [1,2,3]
> two = [[2],[3],[4]]
>
> output = [[2],[6],[12]]
>
> I've written a simple function that does this, however I'm sure there is
> a better way, instead of casting the item to a float.
>
> def multiplyArray(arr1,arr2)
>   newArr = []
>   i=0
>   arr1.each do |x|
>     x = x.to_s.to_f
>     item = arr2[i].to_s.to_f
>     ele = x * item
>     newArr.push(ele)
>     i = i + 1
>    end
>   puts newArr
> end
>
> Thanks,
>
> --
> Posted via http://www.ruby-forum.com/.

[1,2,3].zip([2,3,4]).map{|a,b| a*b}
    ==>[2, 6, 12]