Class: Array

Inherits:
Object show all
Defined in:
lib/keystone/core_ext/blank.rb,
lib/keystone/core_ext/array.rb

Overview

:nodoc:

Instance Method Summary collapse

Instance Method Details

#sample(count = 1) ⇒ Object



20
21
22
23
24
25
26
27
28
# File 'lib/keystone/core_ext/array.rb', line 20

def sample(count=1)
  if count == 1
    return at( rand( size ) )
  elsif count < 1
    return nil
  else
    return sort_by{rand}[0..count-1]
  end
end

#split_by(num) ⇒ Object

TODO active_supportに同様のメソッドが無いかどうかの確認



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/keystone/core_ext/array.rb', line 7

def split_by(num)
  return [] if self.size < 1
  ret = [[]]
  counter = 0
  self.each do |val|
    (counter = 0;ret << []) if counter >= num
    ret[-1] << val
    counter += 1
  end
  return ret
end