Class: Array

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

Instance Method Summary collapse

Instance Method Details

#/(parts) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/madvertise/ext/array.rb', line 15

def / parts
  inject([[]]) do |ary, x|
    ary << [] if [*ary.last].nitems == length / parts
    ary.last << x
    ary
  end
end

#pick(n = 1) ⇒ Object



3
4
5
# File 'lib/madvertise/ext/array.rb', line 3

def pick(n=1)
  Array.new(n) { self[Kernel::rand(size)-1] }
end

#pick_oneObject



7
8
9
# File 'lib/madvertise/ext/array.rb', line 7

def pick_one
  self[Kernel::rand(size)-1]
end

#to_h(&block) ⇒ Object



11
12
13
# File 'lib/madvertise/ext/array.rb', line 11

def to_h(&block)
  Hash[*self.map { |v| [v, block.call(v)] }.flatten]
end