In many ruby projects and even ruby itself I’ve encountered the using of dup method. For example, in some project I’ve met this construction:
class Array
def flush
self.dup.tap { self.clear }
end
end
The question is why we should use dup method i.e. create copy of object instead of just use the same object? Thanks
Use dup or clone if part of the codebase is written by an idiot that doesn’t know you shouldn’t modify objects if you can help it (instead, you should create a new object):
In my case, the idiot is past-me.