Skip to content
Permalink
Browse files

Merge pull request #557 from kostya/index

Array#index
  • Loading branch information
alex committed Mar 31, 2013
2 parents 720a707 + 589a04d commit 7ab8e9c107457639c6c4b4555a30cdd3f87add3a
@@ -320,6 +320,24 @@ def reverse_each(&block)
return self
end

def index(obj = nil, &block)
return self.enum_for(:index) if !obj && !block
each_with_index do |e, i|
return i if obj ? (e == obj) : block.call(e)
end
nil
end

alias :find_index :index

def rindex(obj = nil, &block)
return self.enum_for(:rindex) if !obj && !block
reverse.each_with_index do |e, i|
return length - i - 1 if obj ? (e == obj) : block.call(e)
end
nil
end

def rotate(n = 1)
Array.new(self).rotate!(n)
end

This file was deleted.

This file was deleted.

@@ -1,10 +1 @@
fails:Array#rindex returns the first index backwards from the end where element == to object
fails:Array#rindex returns size-1 if last element == to object
fails:Array#rindex returns 0 if only first element == to object
fails:Array#rindex returns nil if no element == to object
fails:Array#rindex properly handles empty recursive arrays
fails:Array#rindex properly handles recursive arrays
fails:Array#rindex accepts a block instead of an argument
fails:Array#rindex ignore the block if there is an argument
fails:Array#rindex rechecks the array size during iteration
fails:Array#rindex given no argument and no block produces an Enumerator

0 comments on commit 7ab8e9c

Please sign in to comment.