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_elemObject

Returns a randomly chosen element from self.



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

def rand_elem
  self[rand(self.count)]
end

#randomizeObject

randomizes the order of contents in the Array (self)



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

def randomize
  self.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}})


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

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