Step 18: Array Update


We can use bracket syntax similar to that shown above to get items at particular indices to update items at particular indices.

best_programming_languages = ["C++", "Go", "Scala", "JavaScript", "Python"]
best_programming_languages[2] = "Ruby"
puts(best_programming_languages.inspect)

ind = 0


while ind < best_programming_languages.length
    if best_programming_languages[ind] != "Ruby"
        best_programming_languages[ind] = "Ruby"
    end
    ind = ind + 1
end


puts(best_programming_languages.inspect)

Here we also introduced a new method: .inspect. In the previous example, we saw that if we use .puts on an array, it will print out each item of the array individually. .inspect makes our lives a lot easier here because it allows us to print out the array as an array instead of each item individually.