summaryrefslogtreecommitdiff
path: root/enum.c
diff options
context:
space:
mode:
authormatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2005-12-29 12:05:16 +0000
committermatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2005-12-29 12:05:16 +0000
commitde3bff164c3b8405f40d2e89cf726f7e865102d1 (patch)
tree8f24b7069ab459f2829b3c986f9759111c5181e9 /enum.c
parente5226ea394da345a1abf7719d4e0482de89df26e (diff)
* eval.c (rb_mod_define_method): should save safe_level in the
proc object. [ruby-dev:28146] * test/drb/drbtest.rb (DRbService::self.ext_service): increase timeout limit. a patch from Kazuhiro NISHIYAMA <zn at mbf.nifty.com>. [ruby-dev:28132] * eval.c (ev_const_get): fixed a bug in constant reference during instance_eval. [yarv-dev:707] * eval.c (ev_const_defined): ditto. * lib/yaml.rb (YAML::add_domain_type): typo fixed. a patch from Joel VanderWerf <vjoel at path.berkeley.edu>. [ruby-talk:165285] [ruby-core:6995] * ext/digest/sha2/sha2.c (ULL): support AIX C. a patch from Kailden <kailden at gmail.com>. [ruby-core:06984] * ext/syck/rubyext.c (rb_syck_compile): avoid potential memory leak. * ext/syck/rubyext.c (syck_set_ivars): avoid potential memory leak by explicit symbol allocation. * lib/delegate.rb (Delegator::method_missing): should delegate block as well. * lib/cgi.rb (CGI::QueryExtension::MorphingBody): fix criteria to use Tempfile. A fix from Zev Blut <rubyzbibd at ubit.com>. [ruby-core:06076] * string.c: remove global functions work on $_. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9757 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'enum.c')
-rw-r--r--enum.c112
1 files changed, 106 insertions, 6 deletions
diff --git a/enum.c b/enum.c
index 578ea6b4b9..cec178fd1c 100644
--- a/enum.c
+++ b/enum.c
@@ -570,9 +570,9 @@ all_i(VALUE i, VALUE *memo)
* <code>all?</code> will return <code>true</code> only if none of the
* collection members are <code>false</code> or <code>nil</code>.)
*
- * %w{ ant bear cat}.all? {|word| word.length >= 3} #=> true
- * %w{ ant bear cat}.all? {|word| word.length >= 4} #=> false
- * [ nil, true, 99 ].all? #=> false
+ * %w{ant bear cat}.all? {|word| word.length >= 3} #=> true
+ * %w{ant bear cat}.all? {|word| word.length >= 4} #=> false
+ * [ nil, true, 99 ].all? #=> false
*
*/
@@ -617,9 +617,9 @@ any_i(VALUE i, VALUE *memo)
* of the collection members is not <code>false</code> or
* <code>nil</code>.
*
- * %w{ ant bear cat}.any? {|word| word.length >= 3} #=> true
- * %w{ ant bear cat}.any? {|word| word.length >= 4} #=> true
- * [ nil, true, 99 ].any? #=> true
+ * %w{ant bear cat}.any? {|word| word.length >= 3} #=> true
+ * %w{ant bear cat}.any? {|word| word.length >= 4} #=> true
+ * [ nil, true, 99 ].any? #=> true
*
*/
@@ -633,6 +633,104 @@ enum_any(VALUE obj)
}
static VALUE
+one_iter_i(VALUE i, VALUE *memo)
+{
+ if (RTEST(rb_yield(i))) {
+ if (*memo == Qundef) {
+ *memo = Qtrue;
+ }
+ else if (*memo == Qtrue) {
+ *memo = Qfalse;
+ }
+ }
+ return Qnil;
+}
+
+static VALUE
+one_i(VALUE i, VALUE *memo)
+{
+ if (RTEST(i)) {
+ if (*memo == Qundef) {
+ *memo = Qtrue;
+ }
+ else if (*memo == Qtrue) {
+ *memo = Qfalse;
+ }
+ }
+ return Qnil;
+}
+
+/*
+ * call-seq:
+ * enum.one? [{|obj| block }] => true or false
+ *
+ * Passes each element of the collection to the given block. The method
+ * returns <code>true</code> if the block returns <code>true</code>
+ * exactly once. If the block is not given, <code>one?</code> will return
+ * <code>true</code> only if exactly one of the collection members are
+ * true.
+ *
+ * %w{ant bear cat}.one? {|word| word.length == 4} #=> true
+ * %w{ant bear cat}.one? {|word| word.length >= 4} #=> false
+ * [ nil, true, 99 ].one? #=> true
+ *
+ */
+
+static VALUE
+enum_one(VALUE obj)
+{
+ VALUE result = Qundef;
+
+ rb_iterate(rb_each, obj, rb_block_given_p() ? one_iter_i : one_i, (VALUE)&result);
+ if (result == Qundef) return Qfalse;
+ return result;
+}
+
+static VALUE
+none_iter_i(VALUE i, VALUE *memo)
+{
+ if (RTEST(rb_yield(i))) {
+ *memo = Qfalse;
+ rb_iter_break();
+ }
+ return Qnil;
+}
+
+static VALUE
+none_i(VALUE i, VALUE *memo)
+{
+ if (RTEST(i)) {
+ *memo = Qfalse;
+ rb_iter_break();
+ }
+ return Qnil;
+}
+
+/*
+ * call-seq:
+ * enum.none? [{|obj| block }] => true or false
+ *
+ * Passes each element of the collection to the given block. The method
+ * returns <code>true</code> if the block never returns <code>true</code>
+ * for all elements. If the block is not given, <code>one?</code> will return
+ * <code>true</code> only if any of the collection members is true.
+ *
+ * %w{ant bear cat}.one? {|word| word.length == 4} #=> true
+ * %w{ant bear cat}.one? {|word| word.length >= 4} #=> false
+ * [ nil, true, 99 ].one? #=> true
+ *
+ */
+
+static VALUE
+enum_none(VALUE obj)
+{
+ VALUE result = Qtrue;
+
+ rb_iterate(rb_each, obj, rb_block_given_p() ? none_iter_i : none_i, (VALUE)&result);
+ return result;
+}
+
+static VALUE
min_i(VALUE i, VALUE *memo)
{
VALUE cmp;
@@ -998,6 +1096,8 @@ Init_Enumerable(void)
rb_define_method(rb_mEnumerable,"partition", enum_partition, 0);
rb_define_method(rb_mEnumerable,"all?", enum_all, 0);
rb_define_method(rb_mEnumerable,"any?", enum_any, 0);
+ rb_define_method(rb_mEnumerable,"one?", enum_one, 0);
+ rb_define_method(rb_mEnumerable,"none?", enum_none, 0);
rb_define_method(rb_mEnumerable,"min", enum_min, 0);
rb_define_method(rb_mEnumerable,"max", enum_max, 0);
rb_define_method(rb_mEnumerable,"min_by", enum_min_by, 0);