class Array

Extensions to the Array class

Public Instance Methods

delete_one(val=nil) click to toggle source

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
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() click to toggle source

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
def pick_one
  return nil if self.empty?
  self[rand(self.length)]
end
shuffle() click to toggle source
# File lib/rbot/core/utils/extends.rb, line 116
def shuffle
  sort_by { rand }
end
shuffle!() click to toggle source
# File lib/rbot/core/utils/extends.rb, line 123
def shuffle!
  replace shuffle
end