mirror of
https://github.com/mruby/mruby
synced 2026-06-08 16:11:16 +00:00
reimplement Array#delete in ruby
This commit is contained in:
+25
-4
@@ -191,11 +191,32 @@ class Array
|
||||
#
|
||||
# Delete element with index +key+
|
||||
def delete(key, &block)
|
||||
while i = self.index(key)
|
||||
self.delete_at(i)
|
||||
ret = key
|
||||
ret = key
|
||||
i = 0
|
||||
j = 0
|
||||
len = self.length
|
||||
while i < len
|
||||
elem = self[i]
|
||||
|
||||
if key == elem
|
||||
ret = elem
|
||||
i += 1
|
||||
next
|
||||
end
|
||||
|
||||
self[j] = elem if i != j
|
||||
|
||||
i += 1
|
||||
j += 1
|
||||
end
|
||||
return block.call if ret.nil? && block
|
||||
|
||||
if i == j
|
||||
return block.call if block
|
||||
return nil
|
||||
end
|
||||
|
||||
self.replace(self[0...j])
|
||||
|
||||
ret
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user