0

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]
Малъ Скрылевъ
  • 16,187
  • 5
  • 56
  • 69

2 Answers2

7

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 }
Малъ Скрылевъ
  • 16,187
  • 5
  • 56
  • 69
3

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

Alexander Tokarev
  • 2,743
  • 2
  • 20
  • 21