I’m using an .each loop to iterate over an array. I want to change elements in this array, so I tried something like this:
ex = ["el01", "el02", "el03", "el04"]
ex.each do |el|
if (el == "el02")
el = "changed"
end
end
puts ex
but it seems don’t works! It puts me :
el01
el02
el03
el04
I want to know what I did wrong! or if it can’t be done this way, how to do it.
You should use
each:Using
forin Ruby is not recommended, as it simply callseachand it does not introduce a new scope.However, if your intent is to change each element in the array, you should use
map: