summaryrefslogtreecommitdiff
path: root/lib/rbot
diff options
context:
space:
mode:
authorGiuseppe Bilotta <giuseppe.bilotta@gmail.com>2010-12-12 21:36:52 +0100
committerGiuseppe Bilotta <giuseppe.bilotta@gmail.com>2010-12-12 21:36:57 +0100
commit2bf7bc4cbcf61afc2f59ad9a897c4740cdbc39ef (patch)
treefdcceb0e90ed3d369e9236c659d5354cc28377c2 /lib/rbot
parent70b0085a8bd04053cc0325937a21f67ab6efe2a7 (diff)
+ Array#delete_if_at
Allow deletion of an element from the array iff it's at a given position (or in a given range of positions).
Diffstat (limited to 'lib/rbot')
-rw-r--r--lib/rbot/core/utils/extends.rb11
1 files changed, 11 insertions, 0 deletions
diff --git a/lib/rbot/core/utils/extends.rb b/lib/rbot/core/utils/extends.rb
index b1964617..84833d9f 100644
--- a/lib/rbot/core/utils/extends.rb
+++ b/lib/rbot/core/utils/extends.rb
@@ -108,6 +108,17 @@ class ::Array
self.delete_at(index)
end
+ # 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.
+ def delete_if_at(el, pos)
+ idx = self.index(el)
+ if pos === idx
+ self.delete_at(idx)
+ else
+ nil
+ end
+ end
+
# shuffle and shuffle! are defined in Ruby >= 1.8.7
# This method returns a new array with the same items as