Permalink
Browse files

Implement Array#keep_if as per rubyspec

  • Loading branch information...
1 parent c89e236 commit e92404b6cd7a20e8ef735d96fc1ff1d4452b4c41 Orion Edwards [GGL] committed Feb 1, 2012
@@ -782,7 +782,7 @@ public static class IListOps {
#endregion
- #region delete, delete_at, reject, reject!, select!
+ #region delete, delete_at, reject, reject!, select!, keep_if
public static bool Remove(BinaryOpStorage/*!*/ equals, IList/*!*/ self, object item) {
int i = 0;
@@ -885,7 +885,19 @@ public static class IListOps {
object result = DeleteIf(block, self, RubyOps.IsFalse, out changed, out jumped);
return jumped ? result : changed ? self : null;
}
-
+
+ [RubyMethod("keep_if")]
+ public static object KeepIf(BlockParam block, IList/*!*/ self) {
+ // as far as I can tell the only difference between select! and keep_if, is that keep_if returns self if it doesn't modify the array whereas select! returns nil
+ return (block != null) ? KeepIfImpl(block, self) : new Enumerator((_, innerBlock) => KeepIfImpl(innerBlock, self));
+ }
+
+ private static object KeepIfImpl(BlockParam/*!*/ block, IList/*!*/ self) {
+ bool changed, jumped;
+ object result = DeleteIf(block, self, RubyOps.IsFalse, out changed, out jumped);
+ return jumped ? result : self;
+ }
+
private static object DeleteIf(BlockParam/*!*/ block, IList/*!*/ self, Func<object, bool> predicate, out bool changed, out bool jumped) {
Assert.NotNull(block, self);
@@ -8049,6 +8049,11 @@ public sealed class BuiltinsLibraryInitializer : IronRuby.Builtins.LibraryInitia
new Func<IronRuby.Runtime.JoinConversionStorage, IronRuby.Runtime.ConversionStorage<IronRuby.Builtins.MutableString>, System.Collections.IList, System.Object, IronRuby.Builtins.MutableString>(IronRuby.Builtins.IListOps.JoinWithLazySeparatorConversion)
);
+ DefineLibraryMethod(module, "keep_if", 0x51,
+ 0x00000000U,
+ new Func<IronRuby.Runtime.BlockParam, System.Collections.IList, System.Object>(IronRuby.Builtins.IListOps.KeepIf)
+ );
+
DefineLibraryMethod(module, "last", 0x51,
0x00000000U, 0x00010000U,
new Func<System.Collections.IList, System.Object>(IronRuby.Builtins.IListOps.Last),

0 comments on commit e92404b

Please sign in to comment.