Question

What I have is:

p(array)
array.each { |c| c=c*y**z-1 ; z=z+1  }
p(array)

The array is:

[35, 35, 35]

y is 36, z is a counter, c is the value in the array.

Before the formula I get:

[35, 35, 35]

[formula happens]

After the formula:

[35, 35, 35]
Was it helpful?

Solution

To modify array itself use the #map!, instead of #each method of Array. Because the #each method is used only for value enumerations for Array, or other classes that include Enumerable module. Therefore, do as follows:

array.map! { |c| c=c*y**z-1 ; z=z+1 ; c }

OTHER TIPS

The each method in the Array class does not mutate an existing instance. It just iterates over it. You should use collect! method instead.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top