I have an array in ruby and i want to change the values of it’s elements dynamically depending on a particular attribute. Suppose i have an array,
array = [123,134,145,515]
And i want to manipulate this elements like getting all the elements multiplied by a parameter, how can i get it done without having to do it explicitly each time using for loop?
For this, you can use something like the
collectmethod in ruby for arrays.You can write a method which can be called whenever required passing the array and parameter as argument.
For instance you can write a method similar to this ;
Now, depending on the requirement you can define a method like this :
In this case, this would return an array similar to this :