Text of docs.ruby-lang.org › capi › en › master › d9 › d69 › enum_8c_source.html as Styx crawled it on Nov 18, 2026. The page may have changed since. Only its words are kept, without pictures or layout, and only the first part of a long page.

Open the live page

Ruby: enum.c (e5e6cd45db456b2a4d368f2a34598641319df630) Source File

Ruby 4.1.0dev (2026-11-18 revision e5e6cd45db456b2a4d368f2a34598641319df630) enum.c (e5e6cd45db456b2a4d368f2a34598641319df630) 1 /********************************************************************** 2 3 enum.c - 4 5 $Author$ 6 created at: Fri Oct 1 15:15:19 JST 1993 7 8 Copyright (C) 1993-2007 Yukihiro Matsumoto 9 10 **********************************************************************/ 11 12 #include "id.h" 13 #include "internal.h" 14 #include "internal/compar.h" 15 #include "internal/enum.h" 16 #include "internal/hash.h" 17 #include "internal/imemo.h" 18 #include "internal/numeric.h" 19 #include "internal/object.h" 20 #include "internal/proc.h" 21 #include "internal/rational.h" 22 #include "internal/re.h" 23 #include "internal/set.h" 24 #include " ruby/util.h " 25 #include "ruby_assert.h" 26 #include "symbol.h" 27 28 VALUE rb_mEnumerable ; 29 30 static ID id_next; 31 static ID id__alone; 32 static ID id__separator; 33 static ID id_chunk_categorize; 34 static ID id_chunk_enumerable; 35 static ID id_sliceafter_enum; 36 static ID id_sliceafter_pat; 37 static ID id_sliceafter_pred; 38 static ID id_slicebefore_enumerable; 39 static ID id_slicebefore_sep_pat; 40 static ID id_slicebefore_sep_pred; 41 static ID id_slicewhen_enum; 42 static ID id_slicewhen_inverted; 43 static ID id_slicewhen_pred; 44 45 #define id_div idDiv 46 #define id_each idEach 47 #define id_eqq idEqq 48 #define id_cmp idCmp 49 #define id_lshift idLTLT 50 #define id_call idCall 51 #define id_size idSize 52 53 VALUE 54 rb_enum_values_pack( int argc, const VALUE *argv) 55 { 56 if (argc == 0) return Qnil ; 57 if (argc == 1) return argv[0]; 58 return rb_ary_new4 (argc, argv); 59 } 60 61 #define ENUM_WANT_SVALUE() do { \ 62 i = rb_enum_values_pack(argc, argv); \ 63 } while (0) 64 65 static VALUE 66 enum_yield( int argc, VALUE ary) 67 { 68 if (argc > 1) 69 return rb_yield_force_blockarg(ary); 70 if (argc == 1) 71 return rb_yield (ary); 72 return rb_yield_values2 (0, 0); 73 } 74 75 static VALUE 76 enum_yield_array( VALUE ary) 77 { 78 rb_len_t len = RARRAY_LEN (ary); 79 80 if ( len > 1) 81 return rb_yield_force_blockarg(ary); 82 if ( len == 1) 83 return rb_yield ( RARRAY_AREF (ary, 0)); 84 return rb_yield_values2 (0, 0); 85 } 86 87 static VALUE 88 grep_i( RB_BLOCK_CALL_FUNC_ARGLIST (i, args)) 89 { 90 struct MEMO *memo = MEMO_CAST(args); 91 ENUM_WANT_SVALUE(); 92 93 if ( RTEST ( rb_funcallv (memo->v1, id_eqq, 1, &i)) == RTEST (memo->u3.value)) { 94 rb_ary_push (memo->v2, i); 95 } 96 return Qnil ; 97 } 98 99 static VALUE 100 grep_regexp_i( RB_BLOCK_CALL_FUNC_ARGLIST (i, args)) 101 { 102 struct MEMO *memo = MEMO_CAST(args); 103 VALUE converted_element, match; 104 ENUM_WANT_SVALUE(); 105 106 /* In case element can't be converted to a Symbol or String: not a match (don't raise) */ 107 converted_element = SYMBOL_P (i) ? i : rb_check_string_type (i); 108 match = NIL_P (converted_element) ?

Qfalse : rb_reg_match_p(memo->v1, i, 0); 109 if (match == memo->u3.value) { 110 rb_ary_push (memo->v2, i); 111 } 112 return Qnil ; 113 } 114 115 static VALUE 116 grep_iter_i( RB_BLOCK_CALL_FUNC_ARGLIST (i, args)) 117 { 118 struct MEMO *memo = MEMO_CAST(args); 119 ENUM_WANT_SVALUE(); 120 121 if ( RTEST ( rb_funcallv (memo->v1, id_eqq, 1, &i)) == RTEST (memo->u3.value)) { 122 rb_ary_push (memo->v2, enum_yield(argc, i)); 123 } 124 return Qnil ; 125 } 126 127 static VALUE 128 enum_grep0( VALUE obj, VALUE pat, VALUE test) 129 { 130 VALUE ary = rb_ary_new (); 131 struct MEMO *memo = rb_imemo_memo_new_value(pat, ary, test); 132 rb_block_call_func_t fn; 133 if ( rb_block_given_p ()) { 134 fn = grep_iter_i; 135 } 136 else if ( RB_TYPE_P (pat, T_REGEXP ) && 137 LIKELY( rb_method_basic_definition_p ( CLASS_OF (pat), idEqq))) { 138 fn = grep_regexp_i; 139 } 140 else { 141 fn = grep_i; 142 } 143 rb_block_call (obj, id_each, 0, 0, fn, ( VALUE )memo); 144 145 return ary; 146 } 147 148 /* 149 * call-seq: 150 * grep(pattern) -> array 151 * grep(pattern) {|element| ... } -> array 152 * 153 * Returns an array of objects based elements of +self+ that match the given pattern.

154 * 155 * With no block given, returns an array containing each element 156 * for which <tt>pattern === element</tt> is +true+: 157 * 158 * a = ['foo', 'bar', 'car', 'moo'] 159 * a.grep(/ar/) # => ["bar", "car"] 160 * (1..10).grep(3..8) # => [3, 4, 5, 6, 7, 8] 161 * ['a', 'b', 0, 1].grep(Integer) # => [0, 1] 162 * 163 * With a block given, 164 * calls the block with each matching element and returns an array containing each 165 * object returned by the block: 166 * 167 * a = ['foo', 'bar', 'car', 'moo'] 168 * a.grep(/ar/) {|element| element.upcase } # => ["BAR", "CAR"] 169 * 170 * Related: #grep_v.

171 */ 172 173 static VALUE 174 enum_grep( VALUE obj, VALUE pat) 175 { 176 return enum_grep0(obj, pat, Qtrue ); 177 } 178 179 /* 180 * call-seq: 181 * grep_v(pattern) -> array 182 * grep_v(pattern) {|element| ... } -> array 183 * 184 * Returns an array of objects based on elements of +self+ 185 * that <em>don't</em> match the given pattern. 186 * 187 * With no block given, returns an array containing each element 188 * for which <tt>pattern === element</tt> is +false+: 189 * 190 * a = ['foo', 'bar', 'car', 'moo'] 191 * a.grep_v(/ar/) # => ["foo", "moo"] 192 * (1..10).grep_v(3..8) # => [1, 2, 9, 10] 193 * ['a', 'b', 0, 1].grep_v(Integer) # => ["a", "b"] 194 * 195 * With a block given, 196 * calls the block with each non-matching element and returns an array containing each 197 * object returned by the block: 198 * 199 * a = ['foo', 'bar', 'car', 'moo'] 200 * a.grep_v(/ar/) {|element| element.upcase } # => ["FOO", "MOO"] 201 * 202 * Related: #grep.

203 */ 204 205 static VALUE 206 enum_grep_v( VALUE obj, VALUE pat) 207 { 208 return enum_grep0(obj, pat, Qfalse ); 209 } 210 211 static inline void 212 MEMO_V3_SET( struct MEMO *m, VALUE v) 213 { 214 RB_OBJ_WRITE (m, &m->u3.value, v); 215 m->flags |= MEMO_U3_IS_VALUE; 216 } 217 218 static void 219 imemo_count_up( struct MEMO *memo) 220 { 221 if (memo->flags & MEMO_U3_IS_VALUE) { 222 RUBY_ASSERT ( RB_TYPE_P (memo->u3.value, T_BIGNUM )); 223 MEMO_V3_SET(memo, rb_int_succ(memo->u3.value)); 224 } 225 else if (++memo->u3.ucnt == 0) { 226 /* overflow */ 227 MEMO_V3_SET(memo, rb_int_succ( ULEN2NUM ( RB_ULEN_MAX ))); 228 } 229 } 230 231 static VALUE 232 imemo_count_value( struct MEMO *memo) 233 { 234 if (memo->flags & MEMO_U3_IS_VALUE) { 235 RUBY_ASSERT ( RB_TYPE_P (memo->u3.value, T_BIGNUM )); 236 return memo->u3.value; 237 } 238 else { 239 return ULEN2NUM (memo->u3.ucnt); 240 } 241 } 242 243 static VALUE 244 count_i( RB_BLOCK_CALL_FUNC_ARGLIST (i, memop)) 245 { 246 struct MEMO *memo = MEMO_CAST(memop); 247 248 ENUM_WANT_SVALUE(); 249 250 if (rb_equal(i, memo->v1)) { 251 imemo_count_up(memo); 252 } 253 return Qnil ; 254 } 255 256 static VALUE 257 count_iter_i( RB_BLOCK_CALL_FUNC_ARGLIST (i, memop)) 258 { 259 struct MEMO *memo = MEMO_CAST(memop); 260 261 if ( RTEST ( rb_yield_values2 (argc, argv))) { 262 imemo_count_up(memo); 263 } 264 return Qnil ; 265 } 266 267 static VALUE 268 count_all_i( RB_BLOCK_CALL_FUNC_ARGLIST (i, memop)) 269 { 270 struct MEMO *memo = MEMO_CAST(memop); 271 272 imemo_count_up(memo); 273 return Qnil ; 274 } 275 276 /* 277 * call-seq: 278 * count -> integer 279 * count(object) -> integer 280 * count {|element| ... } -> integer 281 * 282 * Returns the count of elements, based on an argument or block criterion, if given.

283 * 284 * With no argument and no block given, returns the number of elements: 285 * 286 * [0, 1, 2].count # => 3 287 * {foo: 0, bar: 1, baz: 2}.count # => 3 288 * 289 * With argument +object+ given, 290 * returns the number of elements that are <tt>==</tt> to +object+: 291 * 292 * [0, 1, 2, 1].count(1) # => 2 293 * 294 * With a block given, calls the block with each element 295 * and returns the number of elements for which the block returns a truthy value: 296 * 297 * [0, 1, 2, 3].count {|element| element < 2} # => 2 298 * {foo: 0, bar: 1, baz: 2}.count {|key, value| value < 2} # => 2 29

The rest of this page is on the live site.