Class: Array

Inherits:
Object show all
Defined in:
lib/rbkb/extends/array.rb

Direct Known Subclasses

Plug::PeerList

Instance Method Summary collapse

Instance Method Details

#rand_elem ⇒ Object

Returns a randomly chosen element from self.



25
26
27
# File 'lib/rbkb/extends/array.rb', line 25

def rand_elem
  self[rand(count)]
end

#randomize ⇒ Object

randomizes the order of contents in the Array (self)



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

def randomize
  sort_by { rand }
end

#to_hash(vals = nil) ⇒ Object

Should be in the std library.

keys = [:one, :two, :three]
vals = [1, 2, 3]

keys.zip(vals).to_hash
#=> {:two=>2, :three=>3, :one=>1}})

keys.to_hash(vals)
#=> {:two=>2, :three=>3, :one=>1}})


12
13
14
15
16
17
# File 'lib/rbkb/extends/array.rb', line 12

def to_hash(vals = nil)
  a = vals ? zip(vals) : self
  a.each_with_object({}) do |i, hash|
    hash[i[0]] = i[1]
  end
end