Class: Array
- Inherits:
-
Object
- Object
- Array
- Defined in:
- /home/apoc/projects/ruby/rbot/lib/rbot/core/utils/extends.rb
Overview
Extensions to the Array class
Direct Known Subclasses
Instance Method Summary (collapse)
-
- (Object) delete_if_at(el, pos)
This method deletes a given object el if it's found at the given position pos.
-
- (Object) delete_one(val = nil)
This method returns a given element from the array, deleting it from the array itself.
-
- (Object) pick_one
This method returns a random element from the array, or nil if the array is empty.
- - (Object) shuffle
- - (Object) shuffle!
Instance Method Details
- (Object) delete_if_at(el, pos)
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.
113 114 115 116 117 118 119 120 |
# File '/home/apoc/projects/ruby/rbot/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 |
- (Object) delete_one(val = nil)
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 '/home/apoc/projects/ruby/rbot/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 |
- (Object) pick_one
This method returns a random element from the array, or nil if the array is empty
90 91 92 93 |
# File '/home/apoc/projects/ruby/rbot/lib/rbot/core/utils/extends.rb', line 90 def pick_one return nil if self.empty? self[rand(self.length)] end |
- (Object) shuffle
127 128 129 |
# File '/home/apoc/projects/ruby/rbot/lib/rbot/core/utils/extends.rb', line 127 def shuffle dup.shuffle! end |
- (Object) shuffle!
131 132 133 134 135 136 137 |
# File '/home/apoc/projects/ruby/rbot/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 |