In Ruby, is there a simple way to multiply every element in an n-dimensional array by a single number?
Such that:
[1,2,3,4,5].multiplied_by 2 == [2,4,6,8,10]
and [[1,2,3],[1,2,3]].multiplied_by 2 == [[2,4,6],[2,4,6]]?
(Obviously I made up the multiplied_by function to distinguish it from *, which appears to concatenate multiple copies of the array, which is unfortunately not what I need).
Thanks!
The long-form equivalent of this is:
It’s not really that complicated. You could always make your
multiply_bymethod:If you want it to multiply recursively, you’ll need to handle that as a special case: