From de3bff164c3b8405f40d2e89cf726f7e865102d1 Mon Sep 17 00:00:00 2001 From: matz Date: Thu, 29 Dec 2005 12:05:16 +0000 Subject: * 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 . [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 . [ruby-talk:165285] [ruby-core:6995] * ext/digest/sha2/sha2.c (ULL): support AIX C. a patch from Kailden . [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 . [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 --- enum.c | 112 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 106 insertions(+), 6 deletions(-) (limited to 'enum.c') 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) * all? will return true only if none of the * collection members are false or nil.) * - * %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 false or * nil. * - * %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 * */ @@ -632,6 +632,104 @@ enum_any(VALUE obj) return result; } +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 true if the block returns true + * exactly once. If the block is not given, one? will return + * true 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 true if the block never returns true + * for all elements. If the block is not given, one? will return + * true 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) { @@ -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); -- cgit v1.2.3