class Array

Public Instance Methods

rand_elem() click to toggle source

Returns a randomly chosen element from self.

# File lib/rbkb/extends/array.rb, line 25
def rand_elem
  self[rand(self.count)]
end
randomize() click to toggle source

randomizes the order of contents in the Array (self)

# File lib/rbkb/extends/array.rb, line 20
def randomize
  self.sort_by{ rand }
end
to_hash(vals=nil) click to toggle source

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}})
# 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