Class: Array

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

Direct Known Subclasses

Sunrise::Plugins

Instance Method Summary collapse

Instance Method Details

#chunk(pieces) ⇒ Object



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

def chunk(pieces)
  q, r = length.divmod(pieces)
  (0..pieces).map { |i| i * q + [r, i].min }.enum_cons(2) \
  .map { |a, b| slice(a...b) }
end

#randomObject



18
19
20
21
# File 'lib/sunrise/core_ext/array.rb', line 18

def random
  idx = Kernel.rand(size)
  at(idx)
end

#shuffleObject



10
11
12
# File 'lib/sunrise/core_ext/array.rb', line 10

def shuffle
  sort_by { rand }
end

#shuffle!Object



14
15
16
# File 'lib/sunrise/core_ext/array.rb', line 14

def shuffle!
  self.replace shuffle
end