Class: Array

Inherits:
Object
  • Object
show all
Defined in:
lib/core_ext/array.rb

Instance Method Summary collapse

Instance Method Details

#randObject



2
3
4
# File 'lib/core_ext/array.rb', line 2

def rand
  self.at(Kernel.rand(self.size))
end

#rand?Boolean

Returns:

  • (Boolean)


6
7
8
# File 'lib/core_ext/array.rb', line 6

def rand?
  (self + [ '' ]).rand
end

#subset(count = nil) ⇒ Object



10
11
12
13
14
15
16
17
18
# File 'lib/core_ext/array.rb', line 10

def subset(count = nil)
  array = self.dup
  if (count ||= Kernel.rand(array.size).next) > array.size / 2
    (array.size - count).times { array.delete_at(Kernel.rand(array.size)) }
    array
  else
    count.times.inject([]) {|result, i| result << array.delete_at(Kernel.rand(array.size)) }
  end
end

#subset?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/core_ext/array.rb', line 20

def subset?
  (i = Kernel.rand(self.size.next)) == self.size ? [] : subset(i.next)
end