| Class | Array |
| In: |
lib/rbot/core/utils/extends.rb
|
| Parent: | Object |
Extensions to the Array class
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.
# File lib/rbot/core/utils/extends.rb, line 100
100: def delete_one(val=nil)
101: return nil if self.empty?
102: if val.nil?
103: index = rand(self.length)
104: else
105: index = self.index(val)
106: return nil unless index
107: end
108: self.delete_at(index)
109: end
This method returns a random element from the array, or nil if the array is empty
# File lib/rbot/core/utils/extends.rb, line 90
90: def pick_one
91: return nil if self.empty?
92: self[rand(self.length)]
93: end