👻

destroy_all vs each(&:destroy)

に公開

destroy_all の内部実装を見て、destroy_all と each(&:destroy) の違いについて考察する

まとめ

動作としてはほぼ、違いはない。

destory_all のコード では、 each(&:destroy) をして、中身の初期化をしている。
素の each(&:destroy) したケースと比べると、初期化の違いが存在するが、データの参照をしなければ、扱いとして変わりはない。

追記

各要素の destory で raise したい場合は、 each(&:destroy!) をすれば良い

Discussion