Class: Array
Overview
Extensions to the Array class
Direct Known Subclasses
Instance Method Summary collapse
-
#delete_one(val = nil) ⇒ Object
This method returns a given element from the array, deleting it from the array itself.
-
#pick_one ⇒ Object
This method returns a random element from the array, or nil if the array is empty.
- #shuffle ⇒ Object
- #shuffle! ⇒ Object
Instance Method Details
#delete_one(val = nil) ⇒ Object
This method returns a given element from the array, deleting it from the array itself. The method returns nil if the element couldn’t be found.
If nil is specified, a random element is returned and deleted.
100 101 102 103 104 105 106 107 108 109 |
# File 'lib/rbot/core/utils/extends.rb', line 100 def delete_one(val=nil) return nil if self.empty? if val.nil? index = rand(self.length) else index = self.index(val) return nil unless index end self.delete_at(index) end |
#pick_one ⇒ Object
This method returns a random element from the array, or nil if the array is empty
90 91 92 93 |
# File 'lib/rbot/core/utils/extends.rb', line 90 def pick_one return nil if self.empty? self[rand(self.length)] end |
#shuffle ⇒ Object
116 117 118 |
# File 'lib/rbot/core/utils/extends.rb', line 116 def shuffle sort_by { rand } end |
#shuffle! ⇒ Object
123 124 125 |
# File 'lib/rbot/core/utils/extends.rb', line 123 def shuffle! replace shuffle end |