The slice
method returns a new Hash containing the selected elements. This preserves the original object.
The extract!
method removes those entries from the original hash and returns the extracted elements. This alters the original object.
Note: In many cases Ruby methods ending in !
alter the object they're called on as opposed to similar methods which do not have that extension where a new object is returned.
The difference requires a careful reading of the documentation where the extract!
method is described as:
Removes and returns the key/value pairs matching the given keys.
Where "removes" here is the differentiator.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…