[macruby-changes] [5259] MacRuby/trunk
source_changes at macosforge.org
source_changes at macosforge.org
Sun Mar 6 23:30:42 PST 2011
Revision: 5259
http://trac.macosforge.org/projects/ruby/changeset/5259
Author: watson1978 at gmail.com
Date: 2011-03-06 23:30:41 -0800 (Sun, 06 Mar 2011)
Log Message:
-----------
Implements the Hash#select!.
Test Script:
{{{
require 'test/unit/assertions.rb'
include Test::Unit::Assertions
hash = { "Alice" => 50, "Bob" => 60, "Carol" => 90, "David" => 40 }
hash.select! {|k, v| v >= 60 }
assert_equal({"Bob"=>60, "Carol"=>90}, hash)
puts :ok
}}}
Modified Paths:
--------------
MacRuby/trunk/hash.c
MacRuby/trunk/spec/frozen/tags/macruby/core/hash/select_tags.txt
Modified: MacRuby/trunk/hash.c
===================================================================
--- MacRuby/trunk/hash.c 2011-03-07 06:11:34 UTC (rev 5258)
+++ MacRuby/trunk/hash.c 2011-03-07 07:30:41 UTC (rev 5259)
@@ -870,18 +870,6 @@
return result;
}
-/*
- * call-seq:
- * hsh.keep_if {| key, value | block } -> hsh
- * hsh.keep_if -> an_enumerator
- *
- * Deletes every key-value pair from <i>hsh</i> for which <i>block</i>
- * evaluates to false.
- *
- * If no block is given, an enumerator is returned instead.
- *
- */
-
static int
keep_if_i(VALUE key, VALUE value, VALUE hash)
{
@@ -895,6 +883,43 @@
return ST_CONTINUE;
}
+/*
+ * call-seq:
+ * hsh.select! {| key, value | block } -> hsh or nil
+ * hsh.select! -> an_enumerator
+ *
+ * Equivalent to <code>Hash#keep_if</code>, but returns
+ * <code>nil</code> if no changes were made.
+ */
+
+static VALUE
+rhash_select_bang(VALUE hash, SEL sel)
+{
+ RETURN_ENUMERATOR(hash, 0, 0);
+ rhash_modify(hash);
+ if (!RHASH(hash)->tbl) {
+ return Qnil;
+ }
+ const long n = rhash_len(hash);
+ rb_hash_foreach(hash, keep_if_i, hash);
+ if (n == rhash_len(hash)) {
+ return Qnil;
+ }
+ return hash;
+}
+
+/*
+ * call-seq:
+ * hsh.keep_if {| key, value | block } -> hsh
+ * hsh.keep_if -> an_enumerator
+ *
+ * Deletes every key-value pair from <i>hsh</i> for which <i>block</i>
+ * evaluates to false.
+ *
+ * If no block is given, an enumerator is returned instead.
+ *
+ */
+
VALUE
rhash_keep_if(VALUE hash, SEL sel)
{
@@ -1866,6 +1891,7 @@
rb_objc_define_method(rb_cRubyHash, "delete_if", rhash_delete_if, 0);
rb_objc_define_method(rb_cRubyHash, "keep_if", rhash_keep_if, 0);
rb_objc_define_method(rb_cRubyHash, "select", rhash_select, 0);
+ rb_objc_define_method(rb_cRubyHash, "select!", rhash_select_bang, 0);
rb_objc_define_method(rb_cRubyHash, "reject", rhash_reject, 0);
rb_objc_define_method(rb_cRubyHash, "reject!", rhash_reject_bang, 0);
rb_objc_define_method(rb_cRubyHash, "clear", rhash_clear, 0);
Modified: MacRuby/trunk/spec/frozen/tags/macruby/core/hash/select_tags.txt
===================================================================
--- MacRuby/trunk/spec/frozen/tags/macruby/core/hash/select_tags.txt 2011-03-07 06:11:34 UTC (rev 5258)
+++ MacRuby/trunk/spec/frozen/tags/macruby/core/hash/select_tags.txt 2011-03-07 07:30:41 UTC (rev 5259)
@@ -1,4 +0,0 @@
-fails:Hash#select! is equivalent to keep_if if changes are made
-fails:Hash#select! returns nil if no changes were made
-fails:Hash#select! raises a RuntimeError if called on a frozen instance that is modified
-fails:Hash#select! raises a RuntimeError if called on a frozen instance that would not be modified
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-changes/attachments/20110306/e2245827/attachment.html>
More information about the macruby-changes
mailing list