]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/commitdiff
+ Array#delete_if_at
authorGiuseppe Bilotta <giuseppe.bilotta@gmail.com>
Sun, 12 Dec 2010 20:36:52 +0000 (21:36 +0100)
committerGiuseppe Bilotta <giuseppe.bilotta@gmail.com>
Sun, 12 Dec 2010 20:36:57 +0000 (21:36 +0100)
Allow deletion of an element from the array iff it's at a given position (or in
a given range of positions).

lib/rbot/core/utils/extends.rb

index b1964617f2b153d6fc9f3df19994b6e5369b7a4f..84833d9f89acde3aa676624c2057b1917eb8cdbd 100644 (file)
@@ -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