class Array

Extensions to the Array class

Public Instance Methods

delete_if_at(el, pos) click to toggle source

This method deletes a given object el if it’s found at the given position pos. The position can be either an integer or a range.

# File lib/rbot/core/utils/extends.rb, line 113
def delete_if_at(el, pos)
  idx = self.index(el)
  if pos === idx
    self.delete_at(idx)
  else
    nil
  end
end
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

This method returns a new array with the same items as the receiver, but shuffled

# File lib/rbot/core/utils/extends.rb, line 127
def shuffle
  dup.shuffle!
end
shuffle!() click to toggle source
# File lib/rbot/core/utils/extends.rb, line 131
def shuffle!
  size.times do |i|
    r = i + Kernel.rand(size - i)
    self[i], self[r] = self[r], self[i]
  end
  self
end