Skip to content
Browse files

Implement Array#rindex when called with no args and no block (reverse…

… index-finding enumerator) as per rubyspec
  • Loading branch information...
1 parent b687d8b commit 62f936c51085207bc484194b15ec4fa1ac7882f7 Orion Edwards [GGL] committed Jan 6, 2012
View
17 Languages/Ruby/Libraries/Extensions/IListOps.cs
@@ -1385,6 +1385,23 @@ public static class IListOps {
}
[RubyMethod("rindex")]
+ public static Enumerator/*!*/ GetReverseIndexEnumerator(IList/*!*/ self) { // when rindex is called with no args and no block, we produce a reverse value enumerator. When the enumerator is each'ed over, if the block returns true, we return the index at which that happened
+ return new Enumerator((_, innerBlock) => {
+ object result;
+ for(int i=self.Count - 1; i >= 0; i--) {
+ if(innerBlock.Yield(self[i], out result)) {
+ return null; // block exited with break or similar
+ }
+ if(RubyOps.IsTrue(result)) {
+ return i;
+ }
+ }
+
+ return null;
+ });
+ }
+
+ [RubyMethod("rindex")]
public static object ReverseIndex([NotNull]BlockParam/*!*/ predicate, IList/*!*/ self) {
foreach (int i in IListOps.ReverseEnumerateIndexes(self)) {
object blockResult;
View
3 Languages/Ruby/Libraries/Initializers.Generated.cs
@@ -8135,7 +8135,8 @@ public sealed class BuiltinsLibraryInitializer : IronRuby.Builtins.LibraryInitia
);
DefineLibraryMethod(module, "rindex", 0x51,
- 0x00000001U, 0x00000000U,
+ 0x00000000U, 0x00000001U, 0x00000000U,
+ new Func<System.Collections.IList, IronRuby.Builtins.Enumerator>(IronRuby.Builtins.IListOps.GetReverseIndexEnumerator),
new Func<IronRuby.Runtime.BlockParam, System.Collections.IList, System.Object>(IronRuby.Builtins.IListOps.ReverseIndex),
new Func<IronRuby.Runtime.BinaryOpStorage, IronRuby.Runtime.BlockParam, System.Collections.IList, System.Object, System.Object>(IronRuby.Builtins.IListOps.ReverseIndex)
);

0 comments on commit 62f936c

Please sign in to comment.
Something went wrong with that request. Please try again.