Class: Array
Direct Known Subclasses
Instance Method Summary collapse
-
#rand_elem ⇒ Object
Returns a randomly chosen element from self.
-
#randomize ⇒ Object
randomizes the order of contents in the Array (self).
-
#to_hash(vals = nil) ⇒ Object
Should be in the std library.
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(self.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 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 |