Class Array
In: lib/array.rb
Parent: Object

Extensions to the Array class. Very simple.

Methods

Public Instance methods

merge arr into a random position into the current array.

  ['a','b','c'].merge_randomly(['d','e'])

[Source]

    # File lib/array.rb, line 19
19:   def merge_randomly arr
20:     # get rid of blank items (compact doesn't handle '')

21:     arr = arr.reject{|x| x.nil? || x == ''}
22:     rand_pos = rand(self.length)
23:     self[0..rand_pos] | arr | self[rand_pos..self.length]
24:   end

pull out a random element from array.

  ['a','b','c'].random

[Source]

   # File lib/array.rb, line 5
5:   def random
6:     self[rand(self.length)]
7:   end

pull a random element out of an array that is not one of the ones specified

[Source]

    # File lib/array.rb, line 27
27:   def random_not_one_of arr
28:     (self - arr).random
29:   end

pull out a random number of elements between min and max from array.

  ['a','b','c'].random_values 1,2

[Source]

    # File lib/array.rb, line 11
11:   def random_values min,max
12:     max = size if max > size
13:     min = 0 if min < 0
14:     Array.new(min+rand(max-min+1)).collect{|x| random}
15:   end

[Validate]