How do I get the following to happen: I want to change the value of an array element which is being referenced between pipe characters in a .each loop.
.each
The each method never changes the object it works on.
each
You should use map! method instead:
map!
x = %w(hello there world) x.map! { |element| if(element == "hello") "hi" # change "hello" to "hi" else element end } puts x # output: [hi there world]