Update_all
Update All replaces every value in a collection.
Overview
update_all updates every value in a collection.
items | [1, 2, 3] :update_all!(items, [9, 9, 9])
items | [1, 2, 3] :update_all!(items, [9, 9, 9])
Result:
[9, 9, 9]
[9, 9, 9]
Equivalent to update
update_all and update perform the same operation.
:update!(items, [9, 9, 9]) :update_all!(items, [9, 9, 9])
:update!(items, [9, 9, 9]) :update_all!(items, [9, 9, 9])
Both replace the entire collection.
Mutation
Like most collection utilities, update_all follows Goblin's bang (!) rules.
:update_allreturns a modified copy:update_all!mutates the original collection
items | [1, 2, 3] :update_all(items, [9, 9, 9])
items | [1, 2, 3] :update_all(items, [9, 9, 9])
The original collection remains unchanged.
:update_all!(items, [9, 9, 9])
:update_all!(items, [9, 9, 9])
The original collection is modified.
Similar Operations In Other Languages
| JavaScript | Python | Java | C# | Ruby | PHP |
|---|---|---|---|---|---|
| assignment | assignment | set() |
assignment | []= |
assignment |
splice() |
slice assignment | replaceAll() |
Replace() |
replace |
array_replace() |
fill() |
list comprehension | update loop | Select() |
map! |
array_map() |
Related Variants
Goblin provides several targeted forms of update: