Sha256: f7a6fa0685a5b9ad8dfa66afa401180462dc8e161bc73432094561e15b334c96

Contents?: true

Size: 1.02 KB

Versions: 8

Compression:

Stored size: 1.02 KB

Contents

# -*- coding: utf-8 -*-

class Array

  unless method_defined? :shuffle

    # Randomly arrange the array items.
    #
    # @return [Array] a new array with the items shuffled.
    #
    # This implementation is optimized for speed, not for memory use.
    # See http://codeidol.com/other/rubyckbk/Arrays/Shuffling-an-Array/
    #
    # @example
    #   list=
    #   list=['a','b','c']
    #   list.shuffle!
    #   list => ['c','a','b']

    def shuffle
      dup.shuffle!
    end

  end

  unless method_defined? :shuffle!

    # Randomly arrange the array items.
    #
    # @return [Array] the array, with its items shuffled.
    #
    # This implementation is optimized for speed, not for memory use.
    # See http://codeidol.com/other/rubyckbk/Arrays/Shuffling-an-Array/
    #
    # @example
    #   list=
    #   list=['a','b','c']
    #   list.shuffle!
    #   list => ['c','a','b']

    def shuffle!
      each_index do |i|
        j = rand(length-i) + i
        self[j], self[i] = self[i], self[j]
      end
    end
  end

end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
sixarm_ruby_ramp-5.0.2 lib/sixarm_ruby_ramp/array/shuffle.rb
sixarm_ruby_ramp-5.0.1 lib/sixarm_ruby_ramp/array/shuffle.rb
sixarm_ruby_ramp-5.0.0 lib/sixarm_ruby_ramp/array/shuffle.rb
sixarm_ruby_ramp-4.2.7 lib/sixarm_ruby_ramp/array/shuffle.rb
sixarm_ruby_ramp-4.2.5 lib/sixarm_ruby_ramp/array/shuffle.rb
sixarm_ruby_ramp-4.2.4 lib/sixarm_ruby_ramp/array/shuffle.rb
sixarm_ruby_ramp-4.2.3 lib/sixarm_ruby_ramp/array/shuffle.rb
sixarm_ruby_ramp-4.2.2 lib/sixarm_ruby_ramp/array/shuffle.rb