Change value of array element which is being referenced in a .each loop?

You can get the result you want using collect! or map! to modify the array in-place:

x = %w(hello there world)
x.collect! { |element|
  (element == "hello") ? "hi" : element
}
puts x

At each iteration, the element is replaced into the array by the value returned by the block.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)