Just about to extend the Array class with the following extension:
class Array
def shuffle!
size.downto(1) { |n| push delete_at(rand(n)) }
self
end
end
However, I was wondering where a good place to keep these sort of extensions. I was thinking environment.rb or putting in its own file in the initializers directory.
I usually follow the ActiveSupport convention, which would be to place them in lib/core_ext/#{class}.rb
- in this case, lib/core_ext/array.rb
. As John Hyland notes, you can then require the file explicitly where needed, or put a require statement in initializers.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With