Ruby: range.c Source File

Ruby 3.2.5p208 (2024-07-26 revision 31d0f1a2e7dbfb60731d1f05b868e1d578cda493)

range.c
1/**********************************************************************
2
3 range.c -
4
5 $Author$
6 created at: Thu Aug 19 17:46:47 JST 1993
7
8 Copyright (C) 1993-2007 Yukihiro Matsumoto
9
10**********************************************************************/
11
12#include "ruby/internal/config.h"
13
14#include <assert.h>
15#include <math.h>
16
17#ifdef HAVE_FLOAT_H
18#include <float.h>
19#endif
20
21#include "id.h"
22#include "internal.h"
23#include "internal/array.h"
24#include "internal/compar.h"
25#include "internal/enum.h"
26#include "internal/enumerator.h"
27#include "internal/error.h"
28#include "internal/numeric.h"
29#include "internal/range.h"
30
31 VALUE rb_cRange;
32static ID id_beg, id_end, id_excl;
33#define id_cmp idCmp
34#define id_succ idSucc
35#define id_min idMin
36#define id_max idMax
37
38static VALUE r_cover_p(VALUE, VALUE, VALUE, VALUE);
39
40#define RANGE_SET_BEG(r, v) (RSTRUCT_SET(r, 0, v))
41#define RANGE_SET_END(r, v) (RSTRUCT_SET(r, 1, v))
42#define RANGE_SET_EXCL(r, v) (RSTRUCT_SET(r, 2, v))
43
44#define EXCL(r) RTEST(RANGE_EXCL(r))
45
46static void
47range_init(VALUE range, VALUE beg, VALUE end, VALUE exclude_end)
48{
49 if ((!FIXNUM_P(beg) || !FIXNUM_P(end)) && !NIL_P(beg) && !NIL_P(end)) {
50 VALUE v;
51
52 v = rb_funcall(beg, id_cmp, 1, end);
53 if (NIL_P(v))
54 rb_raise(rb_eArgError, "bad value for range");
55 }
56
57 RANGE_SET_EXCL(range, exclude_end);
58 RANGE_SET_BEG(range, beg);
59 RANGE_SET_END(range, end);
60
61 if (CLASS_OF(range) == rb_cRange) {
62 rb_obj_freeze(range);
63 }
64}
65
66 VALUE
67rb_range_new(VALUE beg, VALUE end, int exclude_end)
68{
69 VALUE range = rb_obj_alloc(rb_cRange);
70
71 range_init(range, beg, end, RBOOL(exclude_end));
72 return range;
73}
74
75static void
76range_modify(VALUE range)
77{
78 rb_check_frozen(range);
79 /* Ranges are immutable, so that they should be initialized only once. */
80 if (RANGE_EXCL(range) != Qnil) {
81 rb_name_err_raise("`initialize' called twice", range, ID2SYM(idInitialize));
82 }
83}
84
85/*
86 * call-seq:
87 * Range.new(begin, end, exclude_end = false) -> new_range
88 *
89 * Returns a new range based on the given objects +begin+ and +end+.
90 * Optional argument +exclude_end+ determines whether object +end+
91 * is included as the last object in the range:
92 *
93 * Range.new(2, 5).to_a # => [2, 3, 4, 5]
94 * Range.new(2, 5, true).to_a # => [2, 3, 4]
95 * Range.new('a', 'd').to_a # => ["a", "b", "c", "d"]
96 * Range.new('a', 'd', true).to_a # => ["a", "b", "c"]
97 *
98 */
99
100static VALUE
101range_initialize(int argc, VALUE *argv, VALUE range)
102{
103 VALUE beg, end, flags;
104
105 rb_scan_args(argc, argv, "21", &beg, &end, &flags);
106 range_modify(range);
107 range_init(range, beg, end, RBOOL(RTEST(flags)));
108 return Qnil;
109}
110
111/* :nodoc: */
112static VALUE
113range_initialize_copy(VALUE range, VALUE orig)
114{
115 range_modify(range);
116 rb_struct_init_copy(range, orig);
117 return range;
118}
119
120/*
121 * call-seq:
122 * exclude_end? -> true or false
123 *
124 * Returns +true+ if +self+ excludes its end value; +false+ otherwise:
125 *
126 * Range.new(2, 5).exclude_end? # => false
127 * Range.new(2, 5, true).exclude_end? # => true
128 * (2..5).exclude_end? # => false
129 * (2...5).exclude_end? # => true
130 */
131
132static VALUE
133range_exclude_end_p(VALUE range)
134{
135 return RBOOL(EXCL(range));
136}
137
138static VALUE
139recursive_equal(VALUE range, VALUE obj, int recur)
140{
141 if (recur) return Qtrue; /* Subtle! */
142 if (!rb_equal(RANGE_BEG(range), RANGE_BEG(obj)))
143 return Qfalse;
144 if (!rb_equal(RANGE_END(range), RANGE_END(obj)))
145 return Qfalse;
146
147 return RBOOL(EXCL(range) == EXCL(obj));
148}
149
150
151/*
152 * call-seq:
153 * self == other -> true or false
154 *
155 * Returns +true+ if and only if:
156 *
157 * - +other+ is a range.
158 * - <tt>other.begin == self.begin</tt>.
159 * - <tt>other.end == self.end</tt>.
160 * - <tt>other.exclude_end? == self.exclude_end?</tt>.
161 *
162 * Otherwise returns +false+.
163 *
164 * r = (1..5)
165 * r == (1..5) # => true
166 * r = Range.new(1, 5)
167 * r == 'foo' # => false
168 * r == (2..5) # => false
169 * r == (1..4) # => false
170 * r == (1...5) # => false
171 * r == Range.new(1, 5, true) # => false
172 *
173 * Note that even with the same argument, the return values of #== and #eql? can differ:
174 *
175 * (1..2) == (1..2.0) # => true
176 * (1..2).eql? (1..2.0) # => false
177 *
178 * Related: Range#eql?.
179 *
180 */
181
182static VALUE
183range_eq(VALUE range, VALUE obj)
184{
185 if (range == obj)
186 return Qtrue;
187 if (!rb_obj_is_kind_of(obj, rb_cRange))
188 return Qfalse;
189
190 return rb_exec_recursive_paired(recursive_equal, range, obj, obj);
191}
192
193/* compares _a_ and _b_ and returns:
194 * < 0: a < b
195 * = 0: a = b
196 * > 0: a > b or non-comparable
197 */
198static int
199r_less(VALUE a, VALUE b)
200{
201 VALUE r = rb_funcall(a, id_cmp, 1, b);
202
203 if (NIL_P(r))
204 return INT_MAX;
205 return rb_cmpint(r, a, b);
206}
207
208static VALUE
209recursive_eql(VALUE range, VALUE obj, int recur)
210{
211 if (recur) return Qtrue; /* Subtle! */
212 if (!rb_eql(RANGE_BEG(range), RANGE_BEG(obj)))
213 return Qfalse;
214 if (!rb_eql(RANGE_END(range), RANGE_END(obj)))
215 return Qfalse;
216
217 return RBOOL(EXCL(range) == EXCL(obj));
218}
219
220/*
221 * call-seq:
222 * eql?(other) -> true or false
223 *
224 * Returns +true+ if and only if:
225 *
226 * - +other+ is a range.
227 * - <tt>other.begin eql? self.begin</tt>.
228 * - <tt>other.end eql? self.end</tt>.
229 * - <tt>other.exclude_end? == self.exclude_end?</tt>.
230 *
231 * Otherwise returns +false+.
232 *
233 * r = (1..5)
234 * r.eql?(1..5) # => true
235 * r = Range.new(1, 5)
236 * r.eql?('foo') # => false
237 * r.eql?(2..5) # => false
238 * r.eql?(1..4) # => false
239 * r.eql?(1...5) # => false
240 * r.eql?(Range.new(1, 5, true)) # => false
241 *
242 * Note that even with the same argument, the return values of #== and #eql? can differ:
243 *
244 * (1..2) == (1..2.0) # => true
245 * (1..2).eql? (1..2.0) # => false
246 *
247 * Related: Range#==.
248 */
249
250static VALUE
251range_eql(VALUE range, VALUE obj)
252{
253 if (range == obj)
254 return Qtrue;
255 if (!rb_obj_is_kind_of(obj, rb_cRange))
256 return Qfalse;
257 return rb_exec_recursive_paired(recursive_eql, range, obj, obj);
258}
259
260/*
261 * call-seq:
262 * hash -> integer
263 *
264 * Returns the integer hash value for +self+.
265 * Two range objects +r0+ and +r1+ have the same hash value
266 * if and only if <tt>r0.eql?(r1)</tt>.
267 *
268 * Related: Range#eql?, Object#hash.
269 */
270
271static VALUE
272range_hash(VALUE range)
273{
274 st_index_t hash = EXCL(range);
275 VALUE v;
276
277 hash = rb_hash_start(hash);
278 v = rb_hash(RANGE_BEG(range));
279 hash = rb_hash_uint(hash, NUM2LONG(v));
280 v = rb_hash(RANGE_END(range));
281 hash = rb_hash_uint(hash, NUM2LONG(v));
282 hash = rb_hash_uint(hash, EXCL(range) << 24);
283 hash = rb_hash_end(hash);
284
285 return ST2FIX(hash);
286}
287
288static void
289range_each_func(VALUE range, int (*func)(VALUE, VALUE), VALUE arg)
290{
291 int c;
292 VALUE b = RANGE_BEG(range);
293 VALUE e = RANGE_END(range);
294 VALUE v = b;
295
296 if (EXCL(range)) {
297 while (r_less(v, e) < 0) {
298 if ((*func)(v, arg)) break;
299 v = rb_funcallv(v, id_succ, 0, 0);
300 }
301 }
302 else {
303 while ((c = r_less(v, e)) <= 0) {
304 if ((*func)(v, arg)) break;
305 if (!c) break;
306 v = rb_funcallv(v, id_succ, 0, 0);
307 }
308 }
309}
310
311static bool
312step_i_iter(VALUE arg)
313{
314 VALUE *iter = (VALUE *)arg;
315
316 if (FIXNUM_P(iter[0])) {
317 iter[0] -= INT2FIX(1) & ~FIXNUM_FLAG;
318 }
319 else {
320 iter[0] = rb_funcall(iter[0], '-', 1, INT2FIX(1));
321 }
322 if (iter[0] != INT2FIX(0)) return false;
323 iter[0] = iter[1];
324 return true;
325}
326
327static int
328sym_step_i(VALUE i, VALUE arg)
329{
330 if (step_i_iter(arg)) {
331 rb_yield(rb_str_intern(i));
332 }
333 return 0;
334}
335
336static int
337step_i(VALUE i, VALUE arg)
338{
339 if (step_i_iter(arg)) {
340 rb_yield(i);
341 }
342 return 0;
343}
344
345static int
346discrete_object_p(VALUE obj)
347{
348 return rb_respond_to(obj, id_succ);
349}
350
351static int
352linear_object_p(VALUE obj)
353{
354 if (FIXNUM_P(obj) || FLONUM_P(obj)) return TRUE;
355 if (SPECIAL_CONST_P(obj)) return FALSE;
356 switch (BUILTIN_TYPE(obj)) {
357 case T_FLOAT:
358 case T_BIGNUM:
359 return TRUE;
360 default:
361 break;
362 }
363 if (rb_obj_is_kind_of(obj, rb_cNumeric)) return TRUE;
364 if (rb_obj_is_kind_of(obj, rb_cTime)) return TRUE;
365 return FALSE;
366}
367
368static VALUE
369check_step_domain(VALUE step)
370{
371 VALUE zero = INT2FIX(0);
372 int cmp;
373 if (!rb_obj_is_kind_of(step, rb_cNumeric)) {
374 step = rb_to_int(step);
375 }
376 cmp = rb_cmpint(rb_funcallv(step, idCmp, 1, &zero), step, zero);
377 if (cmp < 0) {
378 rb_raise(rb_eArgError, "step can't be negative");
379 }
380 else if (cmp == 0) {
381 rb_raise(rb_eArgError, "step can't be 0");
382 }
383 return step;
384}
385
386static VALUE
387range_step_size(VALUE range, VALUE args, VALUE eobj)
388{
389 VALUE b = RANGE_BEG(range), e = RANGE_END(range);
390 VALUE step = INT2FIX(1);
391 if (args) {
392 step = check_step_domain(RARRAY_AREF(args, 0));
393 }
394
395 if (rb_obj_is_kind_of(b, rb_cNumeric) && rb_obj_is_kind_of(e, rb_cNumeric)) {
396 return ruby_num_interval_step_size(b, e, step, EXCL(range));
397 }
398 return Qnil;
399}
400
401/*
402 * call-seq:
403 * step(n = 1) {|element| ... } -> self
404 * step(n = 1) -> enumerator
405 *
406 * Iterates over the elements of +self+.
407 *
408 * With a block given and no argument,
409 * calls the block each element of the range; returns +self+:
410 *
411 * a = []
412 * (1..5).step {|element| a.push(element) } # => 1..5
413 * a # => [1, 2, 3, 4, 5]
414 * a = []
415 * ('a'..'e').step {|element| a.push(element) } # => "a".."e"
416 * a # => ["a", "b", "c", "d", "e"]
417 *
418 * With a block given and a positive integer argument +n+ given,
419 * calls the block with element +0+, element +n+, element <tt>2n</tt>, and so on:
420 *
421 * a = []
422 * (1..5).step(2) {|element| a.push(element) } # => 1..5
423 * a # => [1, 3, 5]
424 * a = []
425 * ('a'..'e').step(2) {|element| a.push(element) } # => "a".."e"
426 * a # => ["a", "c", "e"]
427 *
428 * With no block given, returns an enumerator,
429 * which will be of class Enumerator::ArithmeticSequence if +self+ is numeric;
430 * otherwise of class Enumerator:
431 *
432 * e = (1..5).step(2) # => ((1..5).step(2))
433 * e.class # => Enumerator::ArithmeticSequence
434 * ('a'..'e').step # => #<Enumerator: ...>
435 *
436 * Related: Range#%.
437 */
438static VALUE
439range_step(int argc, VALUE *argv, VALUE range)
440{
441 VALUE b, e, step, tmp;
442
443 b = RANGE_BEG(range);
444 e = RANGE_END(range);
445 step = (!rb_check_arity(argc, 0, 1) ? INT2FIX(1) : argv[0]);
446
447 if (!rb_block_given_p()) {
448 if (!rb_obj_is_kind_of(step, rb_cNumeric)) {
449 step = rb_to_int(step);
450 }
451 if (rb_equal(step, INT2FIX(0))) {
452 rb_raise(rb_eArgError, "step can't be 0");
453 }
454
455 const VALUE b_num_p = rb_obj_is_kind_of(b, rb_cNumeric);
456 const VALUE e_num_p = rb_obj_is_kind_of(e, rb_cNumeric);
457 if ((b_num_p && (NIL_P(e) || e_num_p)) || (NIL_P(b) && e_num_p)) {
458 return rb_arith_seq_new(range, ID2SYM(rb_frame_this_func()), argc, argv,
459 range_step_size, b, e, step, EXCL(range));
460 }
461
462 RETURN_SIZED_ENUMERATOR(range, argc, argv, range_step_size);
463 }
464
465 step = check_step_domain(step);
466 VALUE iter[2] = {INT2FIX(1), step};
467
468 if (FIXNUM_P(b) && NIL_P(e) && FIXNUM_P(step)) {
469 long i = FIX2LONG(b), unit = FIX2LONG(step);
470 do {
471 rb_yield(LONG2FIX(i));
472 i += unit; /* FIXABLE+FIXABLE never overflow */
473 } while (FIXABLE(i));
474 b = LONG2NUM(i);
475
476 for (;; b = rb_big_plus(b, step))
477 rb_yield(b);
478 }
479 else if (FIXNUM_P(b) && FIXNUM_P(e) && FIXNUM_P(step)) { /* fixnums are special */
480 long end = FIX2LONG(e);
481 long i, unit = FIX2LONG(step);
482
483 if (!EXCL(range))
484 end += 1;
485 i = FIX2LONG(b);
486 while (i < end) {
487 rb_yield(LONG2NUM(i));
488 if (i + unit < i) break;
489 i += unit;
490 }
491
492 }
493 else if (SYMBOL_P(b) && (NIL_P(e) || SYMBOL_P(e))) { /* symbols are special */
494 b = rb_sym2str(b);
495 if (NIL_P(e)) {
496 rb_str_upto_endless_each(b, sym_step_i, (VALUE)iter);
497 }
498 else {
499 rb_str_upto_each(b, rb_sym2str(e), EXCL(range), sym_step_i, (VALUE)iter);
500 }
501 }
502 else if (ruby_float_step(b, e, step, EXCL(range), TRUE)) {
503 /* done */
504 }
505 else if (rb_obj_is_kind_of(b, rb_cNumeric) ||
506 !NIL_P(rb_check_to_integer(b, "to_int")) ||
507 !NIL_P(rb_check_to_integer(e, "to_int"))) {
508 ID op = EXCL(range) ? '<' : idLE;
509 VALUE v = b;
510 int i = 0;
511
512 while (NIL_P(e) || RTEST(rb_funcall(v, op, 1, e))) {
513 rb_yield(v);
514 i++;
515 v = rb_funcall(b, '+', 1, rb_funcall(INT2NUM(i), '*', 1, step));
516 }
517 }
518 else {
519 tmp = rb_check_string_type(b);
520
521 if (!NIL_P(tmp)) {
522 b = tmp;
523 if (NIL_P(e)) {
524 rb_str_upto_endless_each(b, step_i, (VALUE)iter);
525 }
526 else {
527 rb_str_upto_each(b, e, EXCL(range), step_i, (VALUE)iter);
528 }
529 }
530 else {
531 if (!discrete_object_p(b)) {
532 rb_raise(rb_eTypeError, "can't iterate from %s",
533 rb_obj_classname(b));
534 }
535 range_each_func(range, step_i, (VALUE)iter);
536 }
537 }
538 return range;
539}
540
541/*
542 * call-seq:
543 * %(n) {|element| ... } -> self
544 * %(n) -> enumerator
545 *
546 * Iterates over the elements of +self+.
547 *
548 * With a block given, calls the block with selected elements of the range;
549 * returns +self+:
550 *
551 * a = []
552 * (1..5).%(2) {|element| a.push(element) } # => 1..5
553 * a # => [1, 3, 5]
554 * a = []
555 * ('a'..'e').%(2) {|element| a.push(element) } # => "a".."e"
556 * a # => ["a", "c", "e"]
557 *
558 * With no block given, returns an enumerator,
559 * which will be of class Enumerator::ArithmeticSequence if +self+ is numeric;
560 * otherwise of class Enumerator:
561 *
562 * e = (1..5) % 2 # => ((1..5).%(2))
563 * e.class # => Enumerator::ArithmeticSequence
564 * ('a'..'e') % 2 # => #<Enumerator: ...>
565 *
566 * Related: Range#step.
567 */
568static VALUE
569range_percent_step(VALUE range, VALUE step)
570{
571 return range_step(1, &step, range);
572}
573
574#if SIZEOF_DOUBLE == 8 && defined(HAVE_INT64_T)
575union int64_double {
576 int64_t i;
577 double d;
578};
579
580static VALUE
581int64_as_double_to_num(int64_t i)
582{
583 union int64_double convert;
584 if (i < 0) {
585 convert.i = -i;
586 return DBL2NUM(-convert.d);
587 }
588 else {
589 convert.i = i;
590 return DBL2NUM(convert.d);
591 }
592}
593
594static int64_t
595double_as_int64(double d)
596{
597 union int64_double convert;
598 convert.d = fabs(d);
599 return d < 0 ? -convert.i : convert.i;
600}
601#endif
602
603static int
604is_integer_p(VALUE v)
605{
606 ID id_integer_p;
607 VALUE is_int;
608 CONST_ID(id_integer_p, "integer?");
609 is_int = rb_check_funcall(v, id_integer_p, 0, 0);
610 return RTEST(is_int) && !UNDEF_P(is_int);
611}
612
613static VALUE
614bsearch_integer_range(VALUE beg, VALUE end, int excl)
615{
616 VALUE satisfied = Qnil;
617 int smaller;
618
619#define BSEARCH_CHECK(expr) \
620 do { \
621 VALUE val = (expr); \
622 VALUE v = rb_yield(val); \
623 if (FIXNUM_P(v)) { \
624 if (v == INT2FIX(0)) return val; \
625 smaller = (SIGNED_VALUE)v < 0; \
626 } \
627 else if (v == Qtrue) { \
628 satisfied = val; \
629 smaller = 1; \
630 } \
631 else if (!RTEST(v)) { \
632 smaller = 0; \
633 } \
634 else if (rb_obj_is_kind_of(v, rb_cNumeric)) { \
635 int cmp = rb_cmpint(rb_funcall(v, id_cmp, 1, INT2FIX(0)), v, INT2FIX(0)); \
636 if (!cmp) return val; \
637 smaller = cmp < 0; \
638 } \
639 else { \
640 rb_raise(rb_eTypeError, "wrong argument type %"PRIsVALUE \
641 " (must be numeric, true, false or nil)", \
642 rb_obj_class(v)); \
643 } \
644 } while (0)
645
646 VALUE low = rb_to_int(beg);
647 VALUE high = rb_to_int(end);
648 VALUE mid, org_high;
649 ID id_div;
650 CONST_ID(id_div, "div");
651
652 if (excl) high = rb_funcall(high, '-', 1, INT2FIX(1));
653 org_high = high;
654
655 while (rb_cmpint(rb_funcall(low, id_cmp, 1, high), low, high) < 0) {
656 mid = rb_funcall(rb_funcall(high, '+', 1, low), id_div, 1, INT2FIX(2));
657 BSEARCH_CHECK(mid);
658 if (smaller) {
659 high = mid;
660 }
661 else {
662 low = rb_funcall(mid, '+', 1, INT2FIX(1));
663 }
664 }
665 if (rb_equal(low, org_high)) {
666 BSEARCH_CHECK(low);
667 if (!smaller) return Qnil;
668 }
669 return satisfied;
670}
671
672/*
673 * call-seq:
674 * bsearch {|obj| block } -> value
675 *
676 * Returns an element from +self+ selected by a binary search.
677 *
678 * See {Binary Searching}[rdoc-ref:bsearch.rdoc].
679 *
680 */
681
682static VALUE
683range_bsearch(VALUE range)
684{
685 VALUE beg, end, satisfied = Qnil;
686 int smaller;
687
688 /* Implementation notes:
689 * Floats are handled by mapping them to 64 bits integers.
690 * Apart from sign issues, floats and their 64 bits integer have the
691 * same order, assuming they are represented as exponent followed
692 * by the mantissa. This is true with or without implicit bit.
693 *
694 * Finding the average of two ints needs to be careful about
695 * potential overflow (since float to long can use 64 bits)
696 * as well as the fact that -1/2 can be 0 or -1 in C89.
697 *
698 * Note that -0.0 is mapped to the same int as 0.0 as we don't want
699 * (-1...0.0).bsearch to yield -0.0.
700 */
701
702#define BSEARCH(conv) \
703 do { \
704 RETURN_ENUMERATOR(range, 0, 0); \
705 if (EXCL(range)) high--; \
706 org_high = high; \
707 while (low < high) { \
708 mid = ((high < 0) == (low < 0)) ? low + ((high - low) / 2) \
709 : (low < -high) ? -((-1 - low - high)/2 + 1) : (low + high) / 2; \
710 BSEARCH_CHECK(conv(mid)); \
711 if (smaller) { \
712 high = mid; \
713 } \
714 else { \
715 low = mid + 1; \
716 } \
717 } \
718 if (low == org_high) { \
719 BSEARCH_CHECK(conv(low)); \
720 if (!smaller) return Qnil; \
721 } \
722 return satisfied; \
723 } while (0)
724
725
726 beg = RANGE_BEG(range);
727 end = RANGE_END(range);
728
729 if (FIXNUM_P(beg) && FIXNUM_P(end)) {
730 long low = FIX2LONG(beg);
731 long high = FIX2LONG(end);
732 long mid, org_high;
733 BSEARCH(INT2FIX);
734 }
735#if SIZEOF_DOUBLE == 8 && defined(HAVE_INT64_T)
736 else if (RB_FLOAT_TYPE_P(beg) || RB_FLOAT_TYPE_P(end)) {
737 int64_t low = double_as_int64(NIL_P(beg) ? -HUGE_VAL : RFLOAT_VALUE(rb_Float(beg)));
738 int64_t high = double_as_int64(NIL_P(end) ? HUGE_VAL : RFLOAT_VALUE(rb_Float(end)));
739 int64_t mid, org_high;
740 BSEARCH(int64_as_double_to_num);
741 }
742#endif
743 else if (is_integer_p(beg) && is_integer_p(end)) {
744 RETURN_ENUMERATOR(range, 0, 0);
745 return bsearch_integer_range(beg, end, EXCL(range));
746 }
747 else if (is_integer_p(beg) && NIL_P(end)) {
748 VALUE diff = LONG2FIX(1);
749 RETURN_ENUMERATOR(range, 0, 0);
750 while (1) {
751 VALUE mid = rb_funcall(beg, '+', 1, diff);
752 BSEARCH_CHECK(mid);
753 if (smaller) {
754 return bsearch_integer_range(beg, mid, 0);
755 }
756 diff = rb_funcall(diff, '*', 1, LONG2FIX(2));
757 }
758 }
759 else if (NIL_P(beg) && is_integer_p(end)) {
760 VALUE diff = LONG2FIX(-1);
761 RETURN_ENUMERATOR(range, 0, 0);
762 while (1) {
763 VALUE mid = rb_funcall(end, '+', 1, diff);
764 BSEARCH_CHECK(mid);
765 if (!smaller) {
766 return bsearch_integer_range(mid, end, 0);
767 }
768 diff = rb_funcall(diff, '*', 1, LONG2FIX(2));
769 }
770 }
771 else {
772 rb_raise(rb_eTypeError, "can't do binary search for %s", rb_obj_classname(beg));
773 }
774 return range;
775}
776
777static int
778each_i(VALUE v, VALUE arg)
779{
780 rb_yield(v);
781 return 0;
782}
783
784static int
785sym_each_i(VALUE v, VALUE arg)
786{
787 return each_i(rb_str_intern(v), arg);
788}
789
790/*
791 * call-seq:
792 * size -> non_negative_integer or Infinity or nil
793 *
794 * Returns the count of elements in +self+
795 * if both begin and end values are numeric;
796 * otherwise, returns +nil+:
797 *
798 * (1..4).size # => 4
799 * (1...4).size # => 3
800 * (1..).size # => Infinity
801 * ('a'..'z').size #=> nil
802 *
803 * Related: Range#count.
804 */
805
806static VALUE
807range_size(VALUE range)
808{
809 VALUE b = RANGE_BEG(range), e = RANGE_END(range);
810 if (rb_obj_is_kind_of(b, rb_cNumeric)) {
811 if (rb_obj_is_kind_of(e, rb_cNumeric)) {
812 return ruby_num_interval_step_size(b, e, INT2FIX(1), EXCL(range));
813 }
814 if (NIL_P(e)) {
815 return DBL2NUM(HUGE_VAL);
816 }
817 }
818 else if (NIL_P(b)) {
819 if (rb_obj_is_kind_of(e, rb_cNumeric)) {
820 return DBL2NUM(HUGE_VAL);
821 }
822 }
823
824 return Qnil;
825}
826
827/*
828 * call-seq:
829 * to_a -> array
830 *
831 * Returns an array containing the elements in +self+, if a finite collection;
832 * raises an exception otherwise.
833 *
834 * (1..4).to_a # => [1, 2, 3, 4]
835 * (1...4).to_a # => [1, 2, 3]
836 * ('a'..'d').to_a # => ["a", "b", "c", "d"]
837 *
838 * Range#entries is an alias for Range#to_a.
839 */
840
841static VALUE
842range_to_a(VALUE range)
843{
844 if (NIL_P(RANGE_END(range))) {
845 rb_raise(rb_eRangeError, "cannot convert endless range to an array");
846 }
847 return rb_call_super(0, 0);
848}
849
850static VALUE
851range_enum_size(VALUE range, VALUE args, VALUE eobj)
852{
853 return range_size(range);
854}
855
856 RBIMPL_ATTR_NORETURN()
857static void
858range_each_bignum_endless(VALUE beg)
859{
860 for (;; beg = rb_big_plus(beg, INT2FIX(1))) {
861 rb_yield(beg);
862 }
863 UNREACHABLE;
864}
865
866 RBIMPL_ATTR_NORETURN()
867static void
868range_each_fixnum_endless(VALUE beg)
869{
870 for (long i = FIX2LONG(beg); FIXABLE(i); i++) {
871 rb_yield(LONG2FIX(i));
872 }
873
874 range_each_bignum_endless(LONG2NUM(RUBY_FIXNUM_MAX + 1));
875 UNREACHABLE;
876}
877
878static VALUE
879range_each_fixnum_loop(VALUE beg, VALUE end, VALUE range)
880{
881 long lim = FIX2LONG(end) + !EXCL(range);
882 for (long i = FIX2LONG(beg); i < lim; i++) {
883 rb_yield(LONG2FIX(i));
884 }
885 return range;
886}
887
888/*
889 * call-seq:
890 * each {|element| ... } -> self
891 * each -> an_enumerator
892 *
893 * With a block given, passes each element of +self+ to the block:
894 *
895 * a = []
896 * (1..4).each {|element| a.push(element) } # => 1..4
897 * a # => [1, 2, 3, 4]
898 *
899 * Raises an exception unless <tt>self.first.respond_to?(:succ)</tt>.
900 *
901 * With no block given, returns an enumerator.
902 *
903 */
904
905static VALUE
906range_each(VALUE range)
907{
908 VALUE beg, end;
909 long i;
910
911 RETURN_SIZED_ENUMERATOR(range, 0, 0, range_enum_size);
912
913 beg = RANGE_BEG(range);
914 end = RANGE_END(range);
915
916 if (FIXNUM_P(beg) && NIL_P(end)) {
917 range_each_fixnum_endless(beg);
918 }
919 else if (FIXNUM_P(beg) && FIXNUM_P(end)) { /* fixnums are special */
920 return range_each_fixnum_loop(beg, end, range);
921 }
922 else if (RB_INTEGER_TYPE_P(beg) && (NIL_P(end) || RB_INTEGER_TYPE_P(end))) {
923 if (SPECIAL_CONST_P(end) || RBIGNUM_POSITIVE_P(end)) { /* end >= FIXNUM_MIN */
924 if (!FIXNUM_P(beg)) {
925 if (RBIGNUM_NEGATIVE_P(beg)) {
926 do {
927 rb_yield(beg);
928 } while (!FIXNUM_P(beg = rb_big_plus(beg, INT2FIX(1))));
929 if (NIL_P(end)) range_each_fixnum_endless(beg);
930 if (FIXNUM_P(end)) return range_each_fixnum_loop(beg, end, range);
931 }
932 else {
933 if (NIL_P(end)) range_each_bignum_endless(beg);
934 if (FIXNUM_P(end)) return range;
935 }
936 }
937 if (FIXNUM_P(beg)) {
938 i = FIX2LONG(beg);
939 do {
940 rb_yield(LONG2FIX(i));
941 } while (POSFIXABLE(++i));
942 beg = LONG2NUM(i);
943 }
944 ASSUME(!FIXNUM_P(beg));
945 ASSUME(!SPECIAL_CONST_P(end));
946 }
947 if (!FIXNUM_P(beg) && RBIGNUM_SIGN(beg) == RBIGNUM_SIGN(end)) {
948 if (EXCL(range)) {
949 while (rb_big_cmp(beg, end) == INT2FIX(-1)) {
950 rb_yield(beg);
951 beg = rb_big_plus(beg, INT2FIX(1));
952 }
953 }
954 else {
955 VALUE c;
956 while ((c = rb_big_cmp(beg, end)) != INT2FIX(1)) {
957 rb_yield(beg);
958 if (c == INT2FIX(0)) break;
959 beg = rb_big_plus(beg, INT2FIX(1));
960 }
961 }
962 }
963 }
964 else if (SYMBOL_P(beg) && (NIL_P(end) || SYMBOL_P(end))) { /* symbols are special */
965 beg = rb_sym2str(beg);
966 if (NIL_P(end)) {
967 rb_str_upto_endless_each(beg, sym_each_i, 0);
968 }
969 else {
970 rb_str_upto_each(beg, rb_sym2str(end), EXCL(range), sym_each_i, 0);
971 }
972 }
973 else {
974 VALUE tmp = rb_check_string_type(beg);
975
976 if (!NIL_P(tmp)) {
977 if (!NIL_P(end)) {
978 rb_str_upto_each(tmp, end, EXCL(range), each_i, 0);
979 }
980 else {
981 rb_str_upto_endless_each(tmp, each_i, 0);
982 }
983 }
984 else {
985 if (!discrete_object_p(beg)) {
986 rb_raise(rb_eTypeError, "can't iterate from %s",
987 rb_obj_classname(beg));
988 }
989 if (!NIL_P(end))
990 range_each_func(range, each_i, 0);
991 else
992 for (;; beg = rb_funcallv(beg, id_succ, 0, 0))
993 rb_yield(beg);
994 }
995 }
996 return range;
997}
998
999/*
1000 * call-seq:
1001 * self.begin -> object
1002 *
1003 * Returns the object that defines the beginning of +self+.
1004 *
1005 * (1..4).begin # => 1
1006 * (..2).begin # => nil
1007 *
1008 * Related: Range#first, Range#end.
1009 */
1010
1011static VALUE
1012range_begin(VALUE range)
1013{
1014 return RANGE_BEG(range);
1015}
1016
1017
1018/*
1019 * call-seq:
1020 * self.end -> object
1021 *
1022 * Returns the object that defines the end of +self+.
1023 *
1024 * (1..4).end # => 4
1025 * (1...4).end # => 4
1026 * (1..).end # => nil
1027 *
1028 * Related: Range#begin, Range#last.
1029 */
1030
1031
1032static VALUE
1033range_end(VALUE range)
1034{
1035 return RANGE_END(range);
1036}
1037
1038
1039static VALUE
1040first_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, cbarg))
1041{
1042 VALUE *ary = (VALUE *)cbarg;
1043 long n = NUM2LONG(ary[0]);
1044
1045 if (n <= 0) {
1046 rb_iter_break();
1047 }
1048 rb_ary_push(ary[1], i);
1049 n--;
1050 ary[0] = LONG2NUM(n);
1051 return Qnil;
1052}
1053
1054/*
1055 * call-seq:
1056 * first -> object
1057 * first(n) -> array
1058 *
1059 * With no argument, returns the first element of +self+, if it exists:
1060 *
1061 * (1..4).first # => 1
1062 * ('a'..'d').first # => "a"
1063 *
1064 * With non-negative integer argument +n+ given,
1065 * returns the first +n+ elements in an array:
1066 *
1067 * (1..10).first(3) # => [1, 2, 3]
1068 * (1..10).first(0) # => []
1069 * (1..4).first(50) # => [1, 2, 3, 4]
1070 *
1071 * Raises an exception if there is no first element:
1072 *
1073 * (..4).first # Raises RangeError
1074 */
1075
1076static VALUE
1077range_first(int argc, VALUE *argv, VALUE range)
1078{
1079 VALUE n, ary[2];
1080
1081 if (NIL_P(RANGE_BEG(range))) {
1082 rb_raise(rb_eRangeError, "cannot get the first element of beginless range");
1083 }
1084 if (argc == 0) return RANGE_BEG(range);
1085
1086 rb_scan_args(argc, argv, "1", &n);
1087 ary[0] = n;
1088 ary[1] = rb_ary_new2(NUM2LONG(n));
1089 rb_block_call(range, idEach, 0, 0, first_i, (VALUE)ary);
1090
1091 return ary[1];
1092}
1093
1094static VALUE
1095rb_int_range_last(int argc, VALUE *argv, VALUE range)
1096{
1097 static const VALUE ONE = INT2FIX(1);
1098
1099 VALUE b, e, len_1, len, nv, ary;
1100 int x;
1101 long n;
1102
1103 assert(argc > 0);
1104
1105 b = RANGE_BEG(range);
1106 e = RANGE_END(range);
1107 assert(RB_INTEGER_TYPE_P(b) && RB_INTEGER_TYPE_P(e));
1108
1109 x = EXCL(range);
1110
1111 len_1 = rb_int_minus(e, b);
1112 if (x) {
1113 e = rb_int_minus(e, ONE);
1114 len = len_1;
1115 }
1116 else {
1117 len = rb_int_plus(len_1, ONE);
1118 }
1119
1120 if (FIXNUM_ZERO_P(len) || rb_num_negative_p(len)) {
1121 return rb_ary_new_capa(0);
1122 }
1123
1124 rb_scan_args(argc, argv, "1", &nv);
1125 n = NUM2LONG(nv);
1126 if (n < 0) {
1127 rb_raise(rb_eArgError, "negative array size");
1128 }
1129
1130 nv = LONG2NUM(n);
1131 if (RTEST(rb_int_gt(nv, len))) {
1132 nv = len;
1133 n = NUM2LONG(nv);
1134 }
1135
1136 ary = rb_ary_new_capa(n);
1137 b = rb_int_minus(e, nv);
1138 while (n) {
1139 b = rb_int_plus(b, ONE);
1140 rb_ary_push(ary, b);
1141 --n;
1142 }
1143
1144 return ary;
1145}
1146
1147/*
1148 * call-seq:
1149 * last -> object
1150 * last(n) -> array
1151 *
1152 * With no argument, returns the last element of +self+, if it exists:
1153 *
1154 * (1..4).last # => 4
1155 * ('a'..'d').last # => "d"
1156 *
1157 * Note that +last+ with no argument returns the end element of +self+
1158 * even if #exclude_end? is +true+:
1159 *
1160 * (1...4).last # => 4
1161 * ('a'...'d').last # => "d"
1162 *
1163 * With non-negative integer argument +n+ given,
1164 * returns the last +n+ elements in an array:
1165 *
1166 * (1..10).last(3) # => [8, 9, 10]
1167 * (1..10).last(0) # => []
1168 * (1..4).last(50) # => [1, 2, 3, 4]
1169 *
1170 * Note that +last+ with argument does not return the end element of +self+
1171 * if #exclude_end? it +true+:
1172 *
1173 * (1...4).last(3) # => [1, 2, 3]
1174 * ('a'...'d').last(3) # => ["a", "b", "c"]
1175 *
1176 * Raises an exception if there is no last element:
1177 *
1178 * (1..).last # Raises RangeError
1179 *
1180 */
1181
1182static VALUE
1183range_last(int argc, VALUE *argv, VALUE range)
1184{
1185 VALUE b, e;
1186
1187 if (NIL_P(RANGE_END(range))) {
1188 rb_raise(rb_eRangeError, "cannot get the last element of endless range");
1189 }
1190 if (argc == 0) return RANGE_END(range);
1191
1192 b = RANGE_BEG(range);
1193 e = RANGE_END(range);
1194 if (RB_INTEGER_TYPE_P(b) && RB_INTEGER_TYPE_P(e) &&
1195 RB_LIKELY(rb_method_basic_definition_p(rb_cRange, idEach))) {
1196 return rb_int_range_last(argc, argv, range);
1197 }
1198 return rb_ary_last(argc, argv, rb_Array(range));
1199}
1200
1201
1202/*
1203 * call-seq:
1204 * min -> object
1205 * min(n) -> array
1206 * min {|a, b| ... } -> object
1207 * min(n) {|a, b| ... } -> array
1208 *
1209 * Returns the minimum value in +self+,
1210 * using method <tt><=></tt> or a given block for comparison.
1211 *
1212 * With no argument and no block given,
1213 * returns the minimum-valued element of +self+.
1214 *
1215 * (1..4).min # => 1
1216 * ('a'..'d').min # => "a"
1217 * (-4..-1).min # => -4
1218 *
1219 * With non-negative integer argument +n+ given, and no block given,
1220 * returns the +n+ minimum-valued elements of +self+ in an array:
1221 *
1222 * (1..4).min(2) # => [1, 2]
1223 * ('a'..'d').min(2) # => ["a", "b"]
1224 * (-4..-1).min(2) # => [-4, -3]
1225 * (1..4).min(50) # => [1, 2, 3, 4]
1226 *
1227 * If a block is given, it is called:
1228 *
1229 * - First, with the first two element of +self+.
1230 * - Then, sequentially, with the so-far minimum value and the next element of +self+.
1231 *
1232 * To illustrate:
1233 *
1234 * (1..4).min {|a, b| p [a, b]; a <=> b } # => 1
1235 *
1236 * Output:
1237 *
1238 * [2, 1]
1239 * [3, 1]
1240 * [4, 1]
1241 *
1242 * With no argument and a block given,
1243 * returns the return value of the last call to the block:
1244 *
1245 * (1..4).min {|a, b| -(a <=> b) } # => 4
1246 *
1247 * With non-negative integer argument +n+ given, and a block given,
1248 * returns the return values of the last +n+ calls to the block in an array:
1249 *
1250 * (1..4).min(2) {|a, b| -(a <=> b) } # => [4, 3]
1251 * (1..4).min(50) {|a, b| -(a <=> b) } # => [4, 3, 2, 1]
1252 *
1253 * Returns an empty array if +n+ is zero:
1254 *
1255 * (1..4).min(0) # => []
1256 * (1..4).min(0) {|a, b| -(a <=> b) } # => []
1257 *
1258 * Returns +nil+ or an empty array if:
1259 *
1260 * - The begin value of the range is larger than the end value:
1261 *
1262 * (4..1).min # => nil
1263 * (4..1).min(2) # => []
1264 * (4..1).min {|a, b| -(a <=> b) } # => nil
1265 * (4..1).min(2) {|a, b| -(a <=> b) } # => []
1266 *
1267 * - The begin value of an exclusive range is equal to the end value:
1268 *
1269 * (1...1).min # => nil
1270 * (1...1).min(2) # => []
1271 * (1...1).min {|a, b| -(a <=> b) } # => nil
1272 * (1...1).min(2) {|a, b| -(a <=> b) } # => []
1273 *
1274 * Raises an exception if either:
1275 *
1276 * - +self+ is a beginless range: <tt>(..4)</tt>.
1277 * - A block is given and +self+ is an endless range.
1278 *
1279 * Related: Range#max, Range#minmax.
1280 */
1281
1282
1283static VALUE
1284range_min(int argc, VALUE *argv, VALUE range)
1285{
1286 if (NIL_P(RANGE_BEG(range))) {
1287 rb_raise(rb_eRangeError, "cannot get the minimum of beginless range");
1288 }
1289
1290 if (rb_block_given_p()) {
1291 if (NIL_P(RANGE_END(range))) {
1292 rb_raise(rb_eRangeError, "cannot get the minimum of endless range with custom comparison method");
1293 }
1294 return rb_call_super(argc, argv);
1295 }
1296 else if (argc != 0) {
1297 return range_first(argc, argv, range);
1298 }
1299 else {
1300 VALUE b = RANGE_BEG(range);
1301 VALUE e = RANGE_END(range);
1302 int c = NIL_P(e) ? -1 : OPTIMIZED_CMP(b, e);
1303
1304 if (c > 0 || (c == 0 && EXCL(range)))
1305 return Qnil;
1306 return b;
1307 }
1308}
1309
1310/*
1311 * call-seq:
1312 * max -> object
1313 * max(n) -> array
1314 * max {|a, b| ... } -> object
1315 * max(n) {|a, b| ... } -> array
1316 *
1317 * Returns the maximum value in +self+,
1318 * using method <tt><=></tt> or a given block for comparison.
1319 *
1320 * With no argument and no block given,
1321 * returns the maximum-valued element of +self+.
1322 *
1323 * (1..4).max # => 4
1324 * ('a'..'d').max # => "d"
1325 * (-4..-1).max # => -1
1326 *
1327 * With non-negative integer argument +n+ given, and no block given,
1328 * returns the +n+ maximum-valued elements of +self+ in an array:
1329 *
1330 * (1..4).max(2) # => [4, 3]
1331 * ('a'..'d').max(2) # => ["d", "c"]
1332 * (-4..-1).max(2) # => [-1, -2]
1333 * (1..4).max(50) # => [4, 3, 2, 1]
1334 *
1335 * If a block is given, it is called:
1336 *
1337 * - First, with the first two element of +self+.
1338 * - Then, sequentially, with the so-far maximum value and the next element of +self+.
1339 *
1340 * To illustrate:
1341 *
1342 * (1..4).max {|a, b| p [a, b]; a <=> b } # => 4
1343 *
1344 * Output:
1345 *
1346 * [2, 1]
1347 * [3, 2]
1348 * [4, 3]
1349 *
1350 * With no argument and a block given,
1351 * returns the return value of the last call to the block:
1352 *
1353 * (1..4).max {|a, b| -(a <=> b) } # => 1
1354 *
1355 * With non-negative integer argument +n+ given, and a block given,
1356 * returns the return values of the last +n+ calls to the block in an array:
1357 *
1358 * (1..4).max(2) {|a, b| -(a <=> b) } # => [1, 2]
1359 * (1..4).max(50) {|a, b| -(a <=> b) } # => [1, 2, 3, 4]
1360 *
1361 * Returns an empty array if +n+ is zero:
1362 *
1363 * (1..4).max(0) # => []
1364 * (1..4).max(0) {|a, b| -(a <=> b) } # => []
1365 *
1366 * Returns +nil+ or an empty array if:
1367 *
1368 * - The begin value of the range is larger than the end value:
1369 *
1370 * (4..1).max # => nil
1371 * (4..1).max(2) # => []
1372 * (4..1).max {|a, b| -(a <=> b) } # => nil
1373 * (4..1).max(2) {|a, b| -(a <=> b) } # => []
1374 *
1375 * - The begin value of an exclusive range is equal to the end value:
1376 *
1377 * (1...1).max # => nil
1378 * (1...1).max(2) # => []
1379 * (1...1).max {|a, b| -(a <=> b) } # => nil
1380 * (1...1).max(2) {|a, b| -(a <=> b) } # => []
1381 *
1382 * Raises an exception if either:
1383 *
1384 * - +self+ is a endless range: <tt>(1..)</tt>.
1385 * - A block is given and +self+ is a beginless range.
1386 *
1387 * Related: Range#min, Range#minmax.
1388 *
1389 */
1390
1391static VALUE
1392range_max(int argc, VALUE *argv, VALUE range)
1393{
1394 VALUE e = RANGE_END(range);
1395 int nm = FIXNUM_P(e) || rb_obj_is_kind_of(e, rb_cNumeric);
1396
1397 if (NIL_P(RANGE_END(range))) {
1398 rb_raise(rb_eRangeError, "cannot get the maximum of endless range");
1399 }
1400
1401 VALUE b = RANGE_BEG(range);
1402
1403 if (rb_block_given_p() || (EXCL(range) && !nm) || argc) {
1404 if (NIL_P(b)) {
1405 rb_raise(rb_eRangeError, "cannot get the maximum of beginless range with custom comparison method");
1406 }
1407 return rb_call_super(argc, argv);
1408 }
1409 else {
1410 int c = NIL_P(b) ? -1 : OPTIMIZED_CMP(b, e);
1411
1412 if (c > 0)
1413 return Qnil;
1414 if (EXCL(range)) {
1415 if (!RB_INTEGER_TYPE_P(e)) {
1416 rb_raise(rb_eTypeError, "cannot exclude non Integer end value");
1417 }
1418 if (c == 0) return Qnil;
1419 if (!RB_INTEGER_TYPE_P(b)) {
1420 rb_raise(rb_eTypeError, "cannot exclude end value with non Integer begin value");
1421 }
1422 if (FIXNUM_P(e)) {
1423 return LONG2NUM(FIX2LONG(e) - 1);
1424 }
1425 return rb_funcall(e, '-', 1, INT2FIX(1));
1426 }
1427 return e;
1428 }
1429}
1430
1431/*
1432 * call-seq:
1433 * minmax -> [object, object]
1434 * minmax {|a, b| ... } -> [object, object]
1435 *
1436 * Returns a 2-element array containing the minimum and maximum value in +self+,
1437 * either according to comparison method <tt><=></tt> or a given block.
1438 *
1439 * With no block given, returns the minimum and maximum values,
1440 * using <tt><=></tt> for comparison:
1441 *
1442 * (1..4).minmax # => [1, 4]
1443 * (1...4).minmax # => [1, 3]
1444 * ('a'..'d').minmax # => ["a", "d"]
1445 * (-4..-1).minmax # => [-4, -1]
1446 *
1447 * With a block given, the block must return an integer:
1448 *
1449 * - Negative if +a+ is smaller than +b+.
1450 * - Zero if +a+ and +b+ are equal.
1451 * - Positive if +a+ is larger than +b+.
1452 *
1453 * The block is called <tt>self.size</tt> times to compare elements;
1454 * returns a 2-element Array containing the minimum and maximum values from +self+,
1455 * per the block:
1456 *
1457 * (1..4).minmax {|a, b| -(a <=> b) } # => [4, 1]
1458 *
1459 * Returns <tt>[nil, nil]</tt> if:
1460 *
1461 * - The begin value of the range is larger than the end value:
1462 *
1463 * (4..1).minmax # => [nil, nil]
1464 * (4..1).minmax {|a, b| -(a <=> b) } # => [nil, nil]
1465 *
1466 * - The begin value of an exclusive range is equal to the end value:
1467 *
1468 * (1...1).minmax # => [nil, nil]
1469 * (1...1).minmax {|a, b| -(a <=> b) } # => [nil, nil]
1470 *
1471 * Raises an exception if +self+ is a beginless or an endless range.
1472 *
1473 * Related: Range#min, Range#max.
1474 *
1475 */
1476
1477static VALUE
1478range_minmax(VALUE range)
1479{
1480 if (rb_block_given_p()) {
1481 return rb_call_super(0, NULL);
1482 }
1483 return rb_assoc_new(
1484 rb_funcall(range, id_min, 0),
1485 rb_funcall(range, id_max, 0)
1486 );
1487}
1488
1489int
1490rb_range_values(VALUE range, VALUE *begp, VALUE *endp, int *exclp)
1491{
1492 VALUE b, e;
1493 int excl;
1494
1495 if (rb_obj_is_kind_of(range, rb_cRange)) {
1496 b = RANGE_BEG(range);
1497 e = RANGE_END(range);
1498 excl = EXCL(range);
1499 }
1500 else if (RTEST(rb_obj_is_kind_of(range, rb_cArithSeq))) {
1501 return (int)Qfalse;
1502 }
1503 else {
1504 VALUE x;
1505 b = rb_check_funcall(range, id_beg, 0, 0);
1506 if (UNDEF_P(b)) return (int)Qfalse;
1507 e = rb_check_funcall(range, id_end, 0, 0);
1508 if (UNDEF_P(e)) return (int)Qfalse;
1509 x = rb_check_funcall(range, rb_intern("exclude_end?"), 0, 0);
1510 if (UNDEF_P(x)) return (int)Qfalse;
1511 excl = RTEST(x);
1512 }
1513 *begp = b;
1514 *endp = e;
1515 *exclp = excl;
1516 return (int)Qtrue;
1517}
1518
1519/* Extract the components of a Range.
1520 *
1521 * You can use +err+ to control the behavior of out-of-range and exception.
1522 *
1523 * When +err+ is 0 or 2, if the begin offset is greater than +len+,
1524 * it is out-of-range. The +RangeError+ is raised only if +err+ is 2,
1525 * in this case. If +err+ is 0, +Qnil+ will be returned.
1526 *
1527 * When +err+ is 1, the begin and end offsets won't be adjusted even if they
1528 * are greater than +len+. It allows +rb_ary_aset+ extends arrays.
1529 *
1530 * If the begin component of the given range is negative and is too-large
1531 * abstract value, the +RangeError+ is raised only +err+ is 1 or 2.
1532 *
1533 * The case of <code>err = 0</code> is used in item accessing methods such as
1534 * +rb_ary_aref+, +rb_ary_slice_bang+, and +rb_str_aref+.
1535 *
1536 * The case of <code>err = 1</code> is used in Array's methods such as
1537 * +rb_ary_aset+ and +rb_ary_fill+.
1538 *
1539 * The case of <code>err = 2</code> is used in +rb_str_aset+.
1540 */
1541 VALUE
1542rb_range_component_beg_len(VALUE b, VALUE e, int excl,
1543 long *begp, long *lenp, long len, int err)
1544{
1545 long beg, end;
1546
1547 beg = NIL_P(b) ? 0 : NUM2LONG(b);
1548 end = NIL_P(e) ? -1 : NUM2LONG(e);
1549 if (NIL_P(e)) excl = 0;
1550 if (beg < 0) {
1551 beg += len;
1552 if (beg < 0)
1553 goto out_of_range;
1554 }
1555 if (end < 0)
1556 end += len;
1557 if (!excl)
1558 end++; /* include end point */
1559 if (err == 0 || err == 2) {
1560 if (beg > len)
1561 goto out_of_range;
1562 if (end > len)
1563 end = len;
1564 }
1565 len = end - beg;
1566 if (len < 0)
1567 len = 0;
1568
1569 *begp = beg;
1570 *lenp = len;
1571 return Qtrue;
1572
1573 out_of_range:
1574 return Qnil;
1575}
1576
1577 VALUE
1578rb_range_beg_len(VALUE range, long *begp, long *lenp, long len, int err)
1579{
1580 VALUE b, e;
1581 int excl;
1582
1583 if (!rb_range_values(range, &b, &e, &excl))
1584 return Qfalse;
1585
1586 VALUE res = rb_range_component_beg_len(b, e, excl, begp, lenp, len, err);
1587 if (NIL_P(res) && err) {
1588 rb_raise(rb_eRangeError, "%+"PRIsVALUE" out of range", range);
1589 }
1590
1591 return res;
1592}
1593
1594/*
1595 * call-seq:
1596 * to_s -> string
1597 *
1598 * Returns a string representation of +self+,
1599 * including <tt>begin.to_s</tt> and <tt>end.to_s</tt>:
1600 *
1601 * (1..4).to_s # => "1..4"
1602 * (1...4).to_s # => "1...4"
1603 * (1..).to_s # => "1.."
1604 * (..4).to_s # => "..4"
1605 *
1606 * Note that returns from #to_s and #inspect may differ:
1607 *
1608 * ('a'..'d').to_s # => "a..d"
1609 * ('a'..'d').inspect # => "\"a\"..\"d\""
1610 *
1611 * Related: Range#inspect.
1612 *
1613 */
1614
1615static VALUE
1616range_to_s(VALUE range)
1617{
1618 VALUE str, str2;
1619
1620 str = rb_obj_as_string(RANGE_BEG(range));
1621 str2 = rb_obj_as_string(RANGE_END(range));
1622 str = rb_str_dup(str);
1623 rb_str_cat(str, "...", EXCL(range) ? 3 : 2);
1624 rb_str_append(str, str2);
1625
1626 return str;
1627}
1628
1629static VALUE
1630inspect_range(VALUE range, VALUE dummy, int recur)
1631{
1632 VALUE str, str2 = Qundef;
1633
1634 if (recur) {
1635 return rb_str_new2(EXCL(range) ? "(... ... ...)" : "(... .. ...)");
1636 }
1637 if (!NIL_P(RANGE_BEG(range)) || NIL_P(RANGE_END(range))) {
1638 str = rb_str_dup(rb_inspect(RANGE_BEG(range)));
1639 }
1640 else {
1641 str = rb_str_new(0, 0);
1642 }
1643 rb_str_cat(str, "...", EXCL(range) ? 3 : 2);
1644 if (NIL_P(RANGE_BEG(range)) || !NIL_P(RANGE_END(range))) {
1645 str2 = rb_inspect(RANGE_END(range));
1646 }
1647 if (!UNDEF_P(str2)) rb_str_append(str, str2);
1648
1649 return str;
1650}
1651
1652/*
1653 * call-seq:
1654 * inspect -> string
1655 *
1656 * Returns a string representation of +self+,
1657 * including <tt>begin.inspect</tt> and <tt>end.inspect</tt>:
1658 *
1659 * (1..4).inspect # => "1..4"
1660 * (1...4).inspect # => "1...4"
1661 * (1..).inspect # => "1.."
1662 * (..4).inspect # => "..4"
1663 *
1664 * Note that returns from #to_s and #inspect may differ:
1665 *
1666 * ('a'..'d').to_s # => "a..d"
1667 * ('a'..'d').inspect # => "\"a\"..\"d\""
1668 *
1669 * Related: Range#to_s.
1670 *
1671 */
1672
1673
1674static VALUE
1675range_inspect(VALUE range)
1676{
1677 return rb_exec_recursive(inspect_range, range, 0);
1678}
1679
1680static VALUE range_include_internal(VALUE range, VALUE val);
1681static VALUE range_string_cover_internal(VALUE range, VALUE val);
1682 VALUE rb_str_include_range_p(VALUE beg, VALUE end, VALUE val, VALUE exclusive);
1683
1684/*
1685 * call-seq:
1686 * self === object -> true or false
1687 *
1688 * Returns +true+ if +object+ is between <tt>self.begin</tt> and <tt>self.end</tt>.
1689 * +false+ otherwise:
1690 *
1691 * (1..4) === 2 # => true
1692 * (1..4) === 5 # => false
1693 * (1..4) === 'a' # => false
1694 * (1..4) === 4 # => true
1695 * (1...4) === 4 # => false
1696 * ('a'..'d') === 'c' # => true
1697 * ('a'..'d') === 'e' # => false
1698 *
1699 * A case statement uses method <tt>===</tt>, and so:
1700 *
1701 * case 79
1702 * when (1..50)
1703 * "low"
1704 * when (51..75)
1705 * "medium"
1706 * when (76..100)
1707 * "high"
1708 * end # => "high"
1709 *
1710 * case "2.6.5"
1711 * when ..."2.4"
1712 * "EOL"
1713 * when "2.4"..."2.5"
1714 * "maintenance"
1715 * when "2.5"..."3.0"
1716 * "stable"
1717 * when "3.1"..
1718 * "upcoming"
1719 * end # => "stable"
1720 *
1721 */
1722
1723static VALUE
1724range_eqq(VALUE range, VALUE val)
1725{
1726 VALUE ret = range_string_cover_internal(range, val);
1727 if (!UNDEF_P(ret)) return ret;
1728 return r_cover_p(range, RANGE_BEG(range), RANGE_END(range), val);
1729}
1730
1731
1732/*
1733 * call-seq:
1734 * include?(object) -> true or false
1735 *
1736 * Returns +true+ if +object+ is an element of +self+, +false+ otherwise:
1737 *
1738 * (1..4).include?(2) # => true
1739 * (1..4).include?(5) # => false
1740 * (1..4).include?(4) # => true
1741 * (1...4).include?(4) # => false
1742 * ('a'..'d').include?('b') # => true
1743 * ('a'..'d').include?('e') # => false
1744 * ('a'..'d').include?('B') # => false
1745 * ('a'..'d').include?('d') # => true
1746 * ('a'...'d').include?('d') # => false
1747 *
1748 * If begin and end are numeric, #include? behaves like #cover?
1749 *
1750 * (1..3).include?(1.5) # => true
1751 * (1..3).cover?(1.5) # => true
1752 *
1753 * But when not numeric, the two methods may differ:
1754 *
1755 * ('a'..'d').include?('cc') # => false
1756 * ('a'..'d').cover?('cc') # => true
1757 *
1758 * Related: Range#cover?.
1759 *
1760 * Range#member? is an alias for Range#include?.
1761 */
1762
1763static VALUE
1764range_include(VALUE range, VALUE val)
1765{
1766 VALUE ret = range_include_internal(range, val);
1767 if (!UNDEF_P(ret)) return ret;
1768 return rb_call_super(1, &val);
1769}
1770
1771static inline bool
1772range_integer_edge_p(VALUE beg, VALUE end)
1773{
1774 return (!NIL_P(rb_check_to_integer(beg, "to_int")) ||
1775 !NIL_P(rb_check_to_integer(end, "to_int")));
1776}
1777
1778static inline bool
1779range_string_edge_p(VALUE beg, VALUE end)
1780{
1781 return RB_TYPE_P(beg, T_STRING) || RB_TYPE_P(end, T_STRING);
1782}
1783
1784static inline bool
1785range_string_range_p(VALUE beg, VALUE end)
1786{
1787 return RB_TYPE_P(beg, T_STRING) && RB_TYPE_P(end, T_STRING);
1788}
1789
1790static inline VALUE
1791range_include_fallback(VALUE beg, VALUE end, VALUE val)
1792{
1793 if (NIL_P(beg) && NIL_P(end)) {
1794 if (linear_object_p(val)) return Qtrue;
1795 }
1796
1797 if (NIL_P(beg) || NIL_P(end)) {
1798 rb_raise(rb_eTypeError, "cannot determine inclusion in beginless/endless ranges");
1799 }
1800
1801 return Qundef;
1802}
1803
1804static VALUE
1805range_string_cover_internal(VALUE range, VALUE val)
1806{
1807 VALUE beg = RANGE_BEG(range);
1808 VALUE end = RANGE_END(range);
1809 int nv = FIXNUM_P(beg) || FIXNUM_P(end) ||
1810 linear_object_p(beg) || linear_object_p(end);
1811
1812 if (nv || range_integer_edge_p(beg, end)) {
1813 return r_cover_p(range, beg, end, val);
1814 }
1815 else if (range_string_edge_p(beg, end)) {
1816 if (range_string_range_p(beg, end)) {
1817 return r_cover_p(range, beg, end, val);
1818 }
1819 if (NIL_P(beg)) {
1820unbounded_begin:;
1821 VALUE r = rb_funcall(val, id_cmp, 1, end);
1822 if (NIL_P(r)) return Qfalse;
1823 if (RANGE_EXCL(range)) {
1824 return RBOOL(rb_cmpint(r, val, end) < 0);
1825 }
1826 return RBOOL(rb_cmpint(r, val, end) <= 0);
1827 }
1828 else if (NIL_P(end)) {
1829unbounded_end:;
1830 VALUE r = rb_funcall(beg, id_cmp, 1, val);
1831 if (NIL_P(r)) return Qfalse;
1832 return RBOOL(rb_cmpint(r, beg, val) <= 0);
1833 }
1834 }
1835
1836 if (!NIL_P(beg) && NIL_P(end)) {
1837 goto unbounded_end;
1838 }
1839 if (NIL_P(beg) && !NIL_P(end)) {
1840 goto unbounded_begin;
1841 }
1842
1843 return range_include_fallback(beg, end, val);
1844}
1845
1846static VALUE
1847range_include_internal(VALUE range, VALUE val)
1848{
1849 VALUE beg = RANGE_BEG(range);
1850 VALUE end = RANGE_END(range);
1851 int nv = FIXNUM_P(beg) || FIXNUM_P(end) ||
1852 linear_object_p(beg) || linear_object_p(end);
1853
1854 if (nv || range_integer_edge_p(beg, end)) {
1855 return r_cover_p(range, beg, end, val);
1856 }
1857 else if (range_string_range_p(beg, end)) {
1858 return rb_str_include_range_p(beg, end, val, RANGE_EXCL(range));
1859 }
1860
1861 return range_include_fallback(beg, end, val);
1862}
1863
1864static int r_cover_range_p(VALUE range, VALUE beg, VALUE end, VALUE val);
1865
1866/*
1867 * call-seq:
1868 * cover?(object) -> true or false
1869 * cover?(range) -> true or false
1870 *
1871 * Returns +true+ if the given argument is within +self+, +false+ otherwise.
1872 *
1873 * With non-range argument +object+, evaluates with <tt><=</tt> and <tt><</tt>.
1874 *
1875 * For range +self+ with included end value (<tt>#exclude_end? == false</tt>),
1876 * evaluates thus:
1877 *
1878 * self.begin <= object <= self.end
1879 *
1880 * Examples:
1881 *
1882 * r = (1..4)
1883 * r.cover?(1) # => true
1884 * r.cover?(4) # => true
1885 * r.cover?(0) # => false
1886 * r.cover?(5) # => false
1887 * r.cover?('foo') # => false
1888
1889 * r = ('a'..'d')
1890 * r.cover?('a') # => true
1891 * r.cover?('d') # => true
1892 * r.cover?(' ') # => false
1893 * r.cover?('e') # => false
1894 * r.cover?(0) # => false
1895 *
1896 * For range +r+ with excluded end value (<tt>#exclude_end? == true</tt>),
1897 * evaluates thus:
1898 *
1899 * r.begin <= object < r.end
1900 *
1901 * Examples:
1902 *
1903 * r = (1...4)
1904 * r.cover?(1) # => true
1905 * r.cover?(3) # => true
1906 * r.cover?(0) # => false
1907 * r.cover?(4) # => false
1908 * r.cover?('foo') # => false
1909
1910 * r = ('a'...'d')
1911 * r.cover?('a') # => true
1912 * r.cover?('c') # => true
1913 * r.cover?(' ') # => false
1914 * r.cover?('d') # => false
1915 * r.cover?(0) # => false
1916 *
1917 * With range argument +range+, compares the first and last
1918 * elements of +self+ and +range+:
1919 *
1920 * r = (1..4)
1921 * r.cover?(1..4) # => true
1922 * r.cover?(0..4) # => false
1923 * r.cover?(1..5) # => false
1924 * r.cover?('a'..'d') # => false
1925
1926 * r = (1...4)
1927 * r.cover?(1..3) # => true
1928 * r.cover?(1..4) # => false
1929 *
1930 * If begin and end are numeric, #cover? behaves like #include?
1931 *
1932 * (1..3).cover?(1.5) # => true
1933 * (1..3).include?(1.5) # => true
1934 *
1935 * But when not numeric, the two methods may differ:
1936 *
1937 * ('a'..'d').cover?('cc') # => true
1938 * ('a'..'d').include?('cc') # => false
1939 *
1940 * Returns +false+ if either:
1941 *
1942 * - The begin value of +self+ is larger than its end value.
1943 * - An internal call to <tt><=></tt> returns +nil+;
1944 * that is, the operands are not comparable.
1945 *
1946 * Beginless ranges cover all values of the same type before the end,
1947 * excluding the end for exclusive ranges. Beginless ranges cover
1948 * ranges that end before the end of the beginless range, or at the
1949 * end of the beginless range for inclusive ranges.
1950 *
1951 * (..2).cover?(1) # => true
1952 * (..2).cover?(2) # => true
1953 * (..2).cover?(3) # => false
1954 * (...2).cover?(2) # => false
1955 * (..2).cover?("2") # => false
1956 * (..2).cover?(..2) # => true
1957 * (..2).cover?(...2) # => true
1958 * (..2).cover?(.."2") # => false
1959 * (...2).cover?(..2) # => false
1960 *
1961 * Endless ranges cover all values of the same type after the
1962 * beginning. Endless exclusive ranges do not cover endless
1963 * inclusive ranges.
1964 *
1965 * (2..).cover?(1) # => false
1966 * (2..).cover?(3) # => true
1967 * (2...).cover?(3) # => true
1968 * (2..).cover?(2) # => true
1969 * (2..).cover?("2") # => false
1970 * (2..).cover?(2..) # => true
1971 * (2..).cover?(2...) # => true
1972 * (2..).cover?("2"..) # => false
1973 * (2...).cover?(2..) # => false
1974 * (2...).cover?(3...) # => true
1975 * (2...).cover?(3..) # => false
1976 * (3..).cover?(2..) # => false
1977 *
1978 * Ranges that are both beginless and endless cover all values and
1979 * ranges, and return true for all arguments, with the exception that
1980 * beginless and endless exclusive ranges do not cover endless
1981 * inclusive ranges.
1982 *
1983 * (nil...).cover?(Object.new) # => true
1984 * (nil...).cover?(nil...) # => true
1985 * (nil..).cover?(nil...) # => true
1986 * (nil...).cover?(nil..) # => false
1987 * (nil...).cover?(1..) # => false
1988 *
1989 * Related: Range#include?.
1990 *
1991 */
1992
1993static VALUE
1994range_cover(VALUE range, VALUE val)
1995{
1996 VALUE beg, end;
1997
1998 beg = RANGE_BEG(range);
1999 end = RANGE_END(range);
2000
2001 if (rb_obj_is_kind_of(val, rb_cRange)) {
2002 return RBOOL(r_cover_range_p(range, beg, end, val));
2003 }
2004 return r_cover_p(range, beg, end, val);
2005}
2006
2007static VALUE
2008r_call_max(VALUE r)
2009{
2010 return rb_funcallv(r, rb_intern("max"), 0, 0);
2011}
2012
2013static int
2014r_cover_range_p(VALUE range, VALUE beg, VALUE end, VALUE val)
2015{
2016 VALUE val_beg, val_end, val_max;
2017 int cmp_end;
2018
2019 val_beg = RANGE_BEG(val);
2020 val_end = RANGE_END(val);
2021
2022 if (!NIL_P(end) && NIL_P(val_end)) return FALSE;
2023 if (!NIL_P(beg) && NIL_P(val_beg)) return FALSE;
2024 if (!NIL_P(val_beg) && !NIL_P(val_end) && r_less(val_beg, val_end) > (EXCL(val) ? -1 : 0)) return FALSE;
2025 if (!NIL_P(val_beg) && !r_cover_p(range, beg, end, val_beg)) return FALSE;
2026
2027
2028 if (!NIL_P(val_end) && !NIL_P(end)) {
2029 VALUE r_cmp_end = rb_funcall(end, id_cmp, 1, val_end);
2030 if (NIL_P(r_cmp_end)) return FALSE;
2031 cmp_end = rb_cmpint(r_cmp_end, end, val_end);
2032 }
2033 else {
2034 cmp_end = r_less(end, val_end);
2035 }
2036
2037
2038 if (EXCL(range) == EXCL(val)) {
2039 return cmp_end >= 0;
2040 }
2041 else if (EXCL(range)) {
2042 return cmp_end > 0;
2043 }
2044 else if (cmp_end >= 0) {
2045 return TRUE;
2046 }
2047
2048 val_max = rb_rescue2(r_call_max, val, 0, Qnil, rb_eTypeError, (VALUE)0);
2049 if (NIL_P(val_max)) return FALSE;
2050
2051 return r_less(end, val_max) >= 0;
2052}
2053
2054static VALUE
2055r_cover_p(VALUE range, VALUE beg, VALUE end, VALUE val)
2056{
2057 if (NIL_P(beg) || r_less(beg, val) <= 0) {
2058 int excl = EXCL(range);
2059 if (NIL_P(end) || r_less(val, end) <= -excl)
2060 return Qtrue;
2061 }
2062 return Qfalse;
2063}
2064
2065static VALUE
2066range_dumper(VALUE range)
2067{
2068 VALUE v = rb_obj_alloc(rb_cObject);
2069
2070 rb_ivar_set(v, id_excl, RANGE_EXCL(range));
2071 rb_ivar_set(v, id_beg, RANGE_BEG(range));
2072 rb_ivar_set(v, id_end, RANGE_END(range));
2073 return v;
2074}
2075
2076static VALUE
2077range_loader(VALUE range, VALUE obj)
2078{
2079 VALUE beg, end, excl;
2080
2081 if (!RB_TYPE_P(obj, T_OBJECT) || RBASIC(obj)->klass != rb_cObject) {
2082 rb_raise(rb_eTypeError, "not a dumped range object");
2083 }
2084
2085 range_modify(range);
2086 beg = rb_ivar_get(obj, id_beg);
2087 end = rb_ivar_get(obj, id_end);
2088 excl = rb_ivar_get(obj, id_excl);
2089 if (!NIL_P(excl)) {
2090 range_init(range, beg, end, RBOOL(RTEST(excl)));
2091 }
2092 return range;
2093}
2094
2095static VALUE
2096range_alloc(VALUE klass)
2097{
2098 /* rb_struct_alloc_noinit itself should not be used because
2099 * rb_marshal_define_compat uses equality of allocation function */
2100 return rb_struct_alloc_noinit(klass);
2101}
2102
2103/*
2104 * call-seq:
2105 * count -> integer
2106 * count(object) -> integer
2107 * count {|element| ... } -> integer
2108 *
2109 * Returns the count of elements, based on an argument or block criterion, if given.
2110 *
2111 * With no argument and no block given, returns the number of elements:
2112 *
2113 * (1..4).count # => 4
2114 * (1...4).count # => 3
2115 * ('a'..'d').count # => 4
2116 * ('a'...'d').count # => 3
2117 * (1..).count # => Infinity
2118 * (..4).count # => Infinity
2119 *
2120 * With argument +object+, returns the number of +object+ found in +self+,
2121 * which will usually be zero or one:
2122 *
2123 * (1..4).count(2) # => 1
2124 * (1..4).count(5) # => 0
2125 * (1..4).count('a') # => 0
2126 *
2127 * With a block given, calls the block with each element;
2128 * returns the number of elements for which the block returns a truthy value:
2129 *
2130 * (1..4).count {|element| element < 3 } # => 2
2131 *
2132 * Related: Range#size.
2133 */
2134static VALUE
2135range_count(int argc, VALUE *argv, VALUE range)
2136{
2137 if (argc != 0) {
2138 /* It is odd for instance (1...).count(0) to return Infinity. Just let
2139 * it loop. */
2140 return rb_call_super(argc, argv);
2141 }
2142 else if (rb_block_given_p()) {
2143 /* Likewise it is odd for instance (1...).count {|x| x == 0 } to return
2144 * Infinity. Just let it loop. */
2145 return rb_call_super(argc, argv);
2146 }
2147 else if (NIL_P(RANGE_END(range))) {
2148 /* We are confident that the answer is Infinity. */
2149 return DBL2NUM(HUGE_VAL);
2150 }
2151 else if (NIL_P(RANGE_BEG(range))) {
2152 /* We are confident that the answer is Infinity. */
2153 return DBL2NUM(HUGE_VAL);
2154 }
2155 else {
2156 return rb_call_super(argc, argv);
2157 }
2158}
2159
2160/* A \Range object represents a collection of values
2161 * that are between given begin and end values.
2162 *
2163 * You can create an \Range object explicitly with:
2164 *
2165 * - A {range literal}[rdoc-ref:syntax/literals.rdoc@Range+Literals]:
2166 *
2167 * # Ranges that use '..' to include the given end value.
2168 * (1..4).to_a # => [1, 2, 3, 4]
2169 * ('a'..'d').to_a # => ["a", "b", "c", "d"]
2170 * # Ranges that use '...' to exclude the given end value.
2171 * (1...4).to_a # => [1, 2, 3]
2172 * ('a'...'d').to_a # => ["a", "b", "c"]
2173 *
2174 * A range may be created using method Range.new:
2175 *
2176 * # Ranges that by default include the given end value.
2177 * Range.new(1, 4).to_a # => [1, 2, 3, 4]
2178 * Range.new('a', 'd').to_a # => ["a", "b", "c", "d"]
2179 * # Ranges that use third argument +exclude_end+ to exclude the given end value.
2180 * Range.new(1, 4, true).to_a # => [1, 2, 3]
2181 * Range.new('a', 'd', true).to_a # => ["a", "b", "c"]
2182 *
2183 * == Beginless Ranges
2184 *
2185 * A _beginless_ _range_ has a definite end value, but a +nil+ begin value.
2186 * Such a range includes all values up to the end value.
2187 *
2188 * r = (..4) # => nil..4
2189 * r.begin # => nil
2190 * r.include?(-50) # => true
2191 * r.include?(4) # => true
2192 *
2193 * r = (...4) # => nil...4
2194 * r.include?(4) # => false
2195 *
2196 * Range.new(nil, 4) # => nil..4
2197 * Range.new(nil, 4, true) # => nil...4
2198 *
2199 * A beginless range may be used to slice an array:
2200 *
2201 * a = [1, 2, 3, 4]
2202 * r = (..2) # => nil...2
2203 * a[r] # => [1, 2]
2204 *
2205 * \Method +each+ for a beginless range raises an exception.
2206 *
2207 * == Endless Ranges
2208 *
2209 * An _endless_ _range_ has a definite begin value, but a +nil+ end value.
2210 * Such a range includes all values from the begin value.
2211 *
2212 * r = (1..) # => 1..
2213 * r.end # => nil
2214 * r.include?(50) # => true
2215 *
2216 * Range.new(1, nil) # => 1..
2217 *
2218 * The literal for an endless range may be written with either two dots
2219 * or three.
2220 * The range has the same elements, either way.
2221 * But note that the two are not equal:
2222 *
2223 * r0 = (1..) # => 1..
2224 * r1 = (1...) # => 1...
2225 * r0.begin == r1.begin # => true
2226 * r0.end == r1.end # => true
2227 * r0 == r1 # => false
2228 *
2229 * An endless range may be used to slice an array:
2230 *
2231 * a = [1, 2, 3, 4]
2232 * r = (2..) # => 2..
2233 * a[r] # => [3, 4]
2234 *
2235 * \Method +each+ for an endless range calls the given block indefinitely:
2236 *
2237 * a = []
2238 * r = (1..)
2239 * r.each do |i|
2240 * a.push(i) if i.even?
2241 * break if i > 10
2242 * end
2243 * a # => [2, 4, 6, 8, 10]
2244 *
2245 * A range can be both beginless and endless. For literal beginless, endless
2246 * ranges, at least the beginning or end of the range must be given as an
2247 * explicit nil value. It is recommended to use an explicit nil beginning and
2248 * implicit nil end, since that is what Ruby uses for Range#inspect:
2249 *
2250 * (nil..) # => (nil..)
2251 * (..nil) # => (nil..)
2252 * (nil..nil) # => (nil..)
2253 *
2254 * == Ranges and Other Classes
2255 *
2256 * An object may be put into a range if its class implements
2257 * instance method <tt><=></tt>.
2258 * Ruby core classes that do so include Array, Complex, File::Stat,
2259 * Float, Integer, Kernel, Module, Numeric, Rational, String, Symbol, and Time.
2260 *
2261 * Example:
2262 *
2263 * t0 = Time.now # => 2021-09-19 09:22:48.4854986 -0500
2264 * t1 = Time.now # => 2021-09-19 09:22:56.0365079 -0500
2265 * t2 = Time.now # => 2021-09-19 09:23:08.5263283 -0500
2266 * (t0..t2).include?(t1) # => true
2267 * (t0..t1).include?(t2) # => false
2268 *
2269 * A range can be iterated over only if its elements
2270 * implement instance method +succ+.
2271 * Ruby core classes that do so include Integer, String, and Symbol
2272 * (but not the other classes mentioned above).
2273 *
2274 * Iterator methods include:
2275 *
2276 * - In \Range itself: #each, #step, and #%
2277 * - Included from module Enumerable: #each_entry, #each_with_index,
2278 * #each_with_object, #each_slice, #each_cons, and #reverse_each.
2279 *
2280 * Example:
2281 *
2282 * a = []
2283 * (1..4).each {|i| a.push(i) }
2284 * a # => [1, 2, 3, 4]
2285 *
2286 * == Ranges and User-Defined Classes
2287 *
2288 * A user-defined class that is to be used in a range
2289 * must implement instance <tt><=></tt>;
2290 * see Integer#<=>.
2291 * To make iteration available, it must also implement
2292 * instance method +succ+; see Integer#succ.
2293 *
2294 * The class below implements both <tt><=></tt> and +succ+,
2295 * and so can be used both to construct ranges and to iterate over them.
2296 * Note that the Comparable module is included
2297 * so the <tt>==</tt> method is defined in terms of <tt><=></tt>.
2298 *
2299 * # Represent a string of 'X' characters.
2300 * class Xs
2301 * include Comparable
2302 * attr_accessor :length
2303 * def initialize(n)
2304 * @length = n
2305 * end
2306 * def succ
2307 * Xs.new(@length + 1)
2308 * end
2309 * def <=>(other)
2310 * @length <=> other.length
2311 * end
2312 * def to_s
2313 * sprintf "%2d #{inspect}", @length
2314 * end
2315 * def inspect
2316 * 'X' * @length
2317 * end
2318 * end
2319 *
2320 * r = Xs.new(3)..Xs.new(6) #=> XXX..XXXXXX
2321 * r.to_a #=> [XXX, XXXX, XXXXX, XXXXXX]
2322 * r.include?(Xs.new(5)) #=> true
2323 * r.include?(Xs.new(7)) #=> false
2324 *
2325 * == What's Here
2326 *
2327 * First, what's elsewhere. \Class \Range:
2328 *
2329 * - Inherits from {class Object}[rdoc-ref:Object@What-27s+Here].
2330 * - Includes {module Enumerable}[rdoc-ref:Enumerable@What-27s+Here],
2331 * which provides dozens of additional methods.
2332 *
2333 * Here, class \Range provides methods that are useful for:
2334 *
2335 * - {Creating a Range}[rdoc-ref:Range@Methods+for+Creating+a+Range]
2336 * - {Querying}[rdoc-ref:Range@Methods+for+Querying]
2337 * - {Comparing}[rdoc-ref:Range@Methods+for+Comparing]
2338 * - {Iterating}[rdoc-ref:Range@Methods+for+Iterating]
2339 * - {Converting}[rdoc-ref:Range@Methods+for+Converting]
2340 *
2341 * === Methods for Creating a \Range
2342 *
2343 * - ::new: Returns a new range.
2344 *
2345 * === Methods for Querying
2346 *
2347 * - #begin: Returns the begin value given for +self+.
2348 * - #bsearch: Returns an element from +self+ selected by a binary search.
2349 * - #count: Returns a count of elements in +self+.
2350 * - #end: Returns the end value given for +self+.
2351 * - #exclude_end?: Returns whether the end object is excluded.
2352 * - #first: Returns the first elements of +self+.
2353 * - #hash: Returns the integer hash code.
2354 * - #last: Returns the last elements of +self+.
2355 * - #max: Returns the maximum values in +self+.
2356 * - #min: Returns the minimum values in +self+.
2357 * - #minmax: Returns the minimum and maximum values in +self+.
2358 * - #size: Returns the count of elements in +self+.
2359 *
2360 * === Methods for Comparing
2361 *
2362 * - #==: Returns whether a given object is equal to +self+ (uses #==).
2363 * - #===: Returns whether the given object is between the begin and end values.
2364 * - #cover?: Returns whether a given object is within +self+.
2365 * - #eql?: Returns whether a given object is equal to +self+ (uses #eql?).
2366 * - #include? (aliased as #member?): Returns whether a given object
2367 * is an element of +self+.
2368 *
2369 * === Methods for Iterating
2370 *
2371 * - #%: Requires argument +n+; calls the block with each +n+-th element of +self+.
2372 * - #each: Calls the block with each element of +self+.
2373 * - #step: Takes optional argument +n+ (defaults to 1);
2374 calls the block with each +n+-th element of +self+.
2375 *
2376 * === Methods for Converting
2377 *
2378 * - #inspect: Returns a string representation of +self+ (uses #inspect).
2379 * - #to_a (aliased as #entries): Returns elements of +self+ in an array.
2380 * - #to_s: Returns a string representation of +self+ (uses #to_s).
2381 *
2382 */
2383
2384void
2385Init_Range(void)
2386{
2387 id_beg = rb_intern_const("begin");
2388 id_end = rb_intern_const("end");
2389 id_excl = rb_intern_const("excl");
2390
2391 rb_cRange = rb_struct_define_without_accessor(
2392 "Range", rb_cObject, range_alloc,
2393 "begin", "end", "excl", NULL);
2394
2395 rb_include_module(rb_cRange, rb_mEnumerable);
2396 rb_marshal_define_compat(rb_cRange, rb_cObject, range_dumper, range_loader);
2397 rb_define_method(rb_cRange, "initialize", range_initialize, -1);
2398 rb_define_method(rb_cRange, "initialize_copy", range_initialize_copy, 1);
2399 rb_define_method(rb_cRange, "==", range_eq, 1);
2400 rb_define_method(rb_cRange, "===", range_eqq, 1);
2401 rb_define_method(rb_cRange, "eql?", range_eql, 1);
2402 rb_define_method(rb_cRange, "hash", range_hash, 0);
2403 rb_define_method(rb_cRange, "each", range_each, 0);
2404 rb_define_method(rb_cRange, "step", range_step, -1);
2405 rb_define_method(rb_cRange, "%", range_percent_step, 1);
2406 rb_define_method(rb_cRange, "bsearch", range_bsearch, 0);
2407 rb_define_method(rb_cRange, "begin", range_begin, 0);
2408 rb_define_method(rb_cRange, "end", range_end, 0);
2409 rb_define_method(rb_cRange, "first", range_first, -1);
2410 rb_define_method(rb_cRange, "last", range_last, -1);
2411 rb_define_method(rb_cRange, "min", range_min, -1);
2412 rb_define_method(rb_cRange, "max", range_max, -1);
2413 rb_define_method(rb_cRange, "minmax", range_minmax, 0);
2414 rb_define_method(rb_cRange, "size", range_size, 0);
2415 rb_define_method(rb_cRange, "to_a", range_to_a, 0);
2416 rb_define_method(rb_cRange, "entries", range_to_a, 0);
2417 rb_define_method(rb_cRange, "to_s", range_to_s, 0);
2418 rb_define_method(rb_cRange, "inspect", range_inspect, 0);
2419
2420 rb_define_method(rb_cRange, "exclude_end?", range_exclude_end_p, 0);
2421
2422 rb_define_method(rb_cRange, "member?", range_include, 1);
2423 rb_define_method(rb_cRange, "include?", range_include, 1);
2424 rb_define_method(rb_cRange, "cover?", range_cover, 1);
2425 rb_define_method(rb_cRange, "count", range_count, -1);
2426}
assert.h
rb_define_method
#define rb_define_method(klass, mid, func, arity)
Defines klass#mid.
Definition cxxanyargs.hpp:670
rb_include_module
void rb_include_module(VALUE klass, VALUE module)
Includes a module to a class.
Definition class.c:1130
rb_scan_args
int rb_scan_args(int argc, const VALUE *argv, const char *fmt,...)
Retrieves argument from argc and argv to given VALUE references according to the format string.
Definition class.c:2579
rb_block_given_p
int rb_block_given_p(void)
Determines if the current method is given a block.
Definition eval.c:868
rb_str_new2
#define rb_str_new2
Old name of rb_str_new_cstr.
Definition string.h:1675
RB_INTEGER_TYPE_P
#define RB_INTEGER_TYPE_P
Old name of rb_integer_type_p.
Definition value_type.h:87
RFLOAT_VALUE
#define RFLOAT_VALUE
Old name of rb_float_value.
Definition double.h:28
T_STRING
#define T_STRING
Old name of RUBY_T_STRING.
Definition value_type.h:78
Qundef
#define Qundef
Old name of RUBY_Qundef.
Definition special_consts.h:62
INT2FIX
#define INT2FIX
Old name of RB_INT2FIX.
Definition long.h:48
UNREACHABLE
#define UNREACHABLE
Old name of RBIMPL_UNREACHABLE.
Definition assume.h:28
T_FLOAT
#define T_FLOAT
Old name of RUBY_T_FLOAT.
Definition value_type.h:64
ID2SYM
#define ID2SYM
Old name of RB_ID2SYM.
Definition symbol.h:44
T_BIGNUM
#define T_BIGNUM
Old name of RUBY_T_BIGNUM.
Definition value_type.h:57
SPECIAL_CONST_P
#define SPECIAL_CONST_P
Old name of RB_SPECIAL_CONST_P.
Definition special_consts.h:56
CLASS_OF
#define CLASS_OF
Old name of rb_class_of.
Definition globals.h:203
FIXABLE
#define FIXABLE
Old name of RB_FIXABLE.
Definition fixnum.h:25
LONG2FIX
#define LONG2FIX
Old name of RB_INT2FIX.
Definition long.h:49
ASSUME
#define ASSUME
Old name of RBIMPL_ASSUME.
Definition assume.h:27
LONG2NUM
#define LONG2NUM
Old name of RB_LONG2NUM.
Definition long.h:50
FLONUM_P
#define FLONUM_P
Old name of RB_FLONUM_P.
Definition special_consts.h:67
Qtrue
#define Qtrue
Old name of RUBY_Qtrue.
Definition special_consts.h:61
ST2FIX
#define ST2FIX
Old name of RB_ST2FIX.
Definition st_data_t.h:33
INT2NUM
#define INT2NUM
Old name of RB_INT2NUM.
Definition int.h:43
Qnil
#define Qnil
Old name of RUBY_Qnil.
Definition special_consts.h:60
Qfalse
#define Qfalse
Old name of RUBY_Qfalse.
Definition special_consts.h:59
FIX2LONG
#define FIX2LONG
Old name of RB_FIX2LONG.
Definition long.h:46
T_OBJECT
#define T_OBJECT
Old name of RUBY_T_OBJECT.
Definition value_type.h:75
NIL_P
#define NIL_P
Old name of RB_NIL_P.
Definition special_consts.h:55
POSFIXABLE
#define POSFIXABLE
Old name of RB_POSFIXABLE.
Definition fixnum.h:29
DBL2NUM
#define DBL2NUM
Old name of rb_float_new.
Definition double.h:29
BUILTIN_TYPE
#define BUILTIN_TYPE
Old name of RB_BUILTIN_TYPE.
Definition value_type.h:85
NUM2LONG
#define NUM2LONG
Old name of RB_NUM2LONG.
Definition long.h:51
FIXNUM_P
#define FIXNUM_P
Old name of RB_FIXNUM_P.
Definition special_consts.h:53
CONST_ID
#define CONST_ID
Old name of RUBY_CONST_ID.
Definition symbol.h:47
rb_ary_new2
#define rb_ary_new2
Old name of rb_ary_new_capa.
Definition array.h:651
SYMBOL_P
#define SYMBOL_P
Old name of RB_SYMBOL_P.
Definition value_type.h:88
rb_raise
void rb_raise(VALUE exc, const char *fmt,...)
Exception entry point.
Definition error.c:3150
rb_iter_break
void rb_iter_break(void)
Breaks from a block.
Definition vm.c:1901
rb_eRangeError
VALUE rb_eRangeError
RangeError exception.
Definition error.c:1095
rb_eTypeError
VALUE rb_eTypeError
TypeError exception.
Definition error.c:1091
rb_eArgError
VALUE rb_eArgError
ArgumentError exception.
Definition error.c:1092
rb_cTime
VALUE rb_cTime
Time class.
Definition time.c:672
rb_Float
VALUE rb_Float(VALUE val)
This is the logic behind Kernel#Float.
Definition object.c:3533
rb_obj_alloc
VALUE rb_obj_alloc(VALUE klass)
Allocates an instance of the given class.
Definition object.c:1940
rb_mEnumerable
VALUE rb_mEnumerable
Enumerable module.
Definition enum.c:27
rb_eql
int rb_eql(VALUE lhs, VALUE rhs)
Checks for equality of the passed objects, in terms of Object#eql?.
Definition object.c:136
rb_cNumeric
VALUE rb_cNumeric
Numeric class.
Definition numeric.c:190
rb_Array
VALUE rb_Array(VALUE val)
This is the logic behind Kernel#Array.
Definition object.c:3688
rb_inspect
VALUE rb_inspect(VALUE obj)
Generates a human-readable textual representation of the given object.
Definition object.c:601
rb_cRange
VALUE rb_cRange
Range class.
Definition range.c:31
rb_equal
VALUE rb_equal(VALUE lhs, VALUE rhs)
This function is an optimised version of calling #==.
Definition object.c:123
rb_obj_is_kind_of
VALUE rb_obj_is_kind_of(VALUE obj, VALUE klass)
Queries if the given object is an instance (of possibly descendants) of the given class.
Definition object.c:788
rb_obj_freeze
VALUE rb_obj_freeze(VALUE obj)
Just calls rb_obj_freeze_inline() inside.
Definition object.c:1183
rb_check_to_integer
VALUE rb_check_to_integer(VALUE val, const char *mid)
Identical to rb_check_convert_type(), except the return value type is fixed to rb_cInteger.
Definition object.c:3014
rb_to_int
VALUE rb_to_int(VALUE val)
Identical to rb_check_to_int(), except it raises in case of conversion mismatch.
Definition object.c:3027
RUBY_FIXNUM_MAX
#define RUBY_FIXNUM_MAX
Maximum possible value that a fixnum can represent.
Definition fixnum.h:55
rb_funcall
VALUE rb_funcall(VALUE recv, ID mid, int n,...)
Calls a method.
Definition vm_eval.c:1102
rb_call_super
VALUE rb_call_super(int argc, const VALUE *argv)
This resembles ruby's super.
Definition vm_eval.c:339
RETURN_SIZED_ENUMERATOR
#define RETURN_SIZED_ENUMERATOR(obj, argc, argv, size_fn)
This roughly resembles return enum_for(__callee__) unless block_given?.
Definition enumerator.h:206
RETURN_ENUMERATOR
#define RETURN_ENUMERATOR(obj, argc, argv)
Identical to RETURN_SIZED_ENUMERATOR(), except its size is unknown.
Definition enumerator.h:239
rb_check_frozen
#define rb_check_frozen
Just another name of rb_check_frozen.
Definition error.h:264
rb_check_arity
static int rb_check_arity(int argc, int min, int max)
Ensures that the passed integer is in the passed range.
Definition error.h:280
rb_range_values
int rb_range_values(VALUE range, VALUE *begp, VALUE *endp, int *exclp)
Deconstructs a range into its components.
Definition range.c:1490
rb_range_new
VALUE rb_range_new(VALUE beg, VALUE end, int excl)
Creates a new Range.
Definition range.c:67
rb_range_beg_len
VALUE rb_range_beg_len(VALUE range, long *begp, long *lenp, long len, int err)
Deconstructs a numerical range.
Definition range.c:1578
rb_hash_uint
#define rb_hash_uint(h, i)
Just another name of st_hash_uint.
Definition string.h:942
rb_hash_end
#define rb_hash_end(h)
Just another name of st_hash_end.
Definition string.h:945
rb_str_append
VALUE rb_str_append(VALUE dst, VALUE src)
Identical to rb_str_buf_append(), except it converts the right hand side before concatenating.
Definition string.c:3353
rb_str_new
#define rb_str_new(str, len)
Allocates an instance of rb_cString.
Definition string.h:1498
rb_str_dup
VALUE rb_str_dup(VALUE str)
Duplicates a string.
Definition string.c:1834
rb_str_cat
VALUE rb_str_cat(VALUE dst, const char *src, long srclen)
Destructively appends the passed contents to the string.
Definition string.c:3177
rb_hash_start
st_index_t rb_hash_start(st_index_t i)
Starts a series of hashing.
Definition random.c:1735
rb_check_string_type
VALUE rb_check_string_type(VALUE obj)
Try converting an object to its stringised representation using its to_str method,...
Definition string.c:2640
rb_str_intern
VALUE rb_str_intern(VALUE str)
Identical to rb_to_symbol(), except it assumes the receiver being an instance of RString.
Definition symbol.c:851
rb_obj_as_string
VALUE rb_obj_as_string(VALUE obj)
Try converting an object to its stringised representation using its to_s method, if any.
Definition string.c:1682
rb_struct_define_without_accessor
VALUE rb_struct_define_without_accessor(const char *name, VALUE super, rb_alloc_func_t func,...)
Identical to rb_struct_define(), except it does not define accessor methods.
Definition struct.c:476
rb_struct_alloc_noinit
VALUE rb_struct_alloc_noinit(VALUE klass)
Allocates an instance of the given class.
Definition struct.c:406
rb_exec_recursive
VALUE rb_exec_recursive(VALUE(*f)(VALUE g, VALUE h, int r), VALUE g, VALUE h)
"Recursion" API entry point.
Definition thread.c:5237
rb_exec_recursive_paired
VALUE rb_exec_recursive_paired(VALUE(*f)(VALUE g, VALUE h, int r), VALUE g, VALUE p, VALUE h)
Identical to rb_exec_recursive(), except it checks for the recursion on the ordered pair of { g,...
Definition thread.c:5248
rb_ivar_set
VALUE rb_ivar_set(VALUE obj, ID name, VALUE val)
Identical to rb_iv_set(), except it accepts the name as an ID instead of a C string.
Definition variable.c:1606
rb_ivar_get
VALUE rb_ivar_get(VALUE obj, ID name)
Identical to rb_iv_get(), except it accepts the name as an ID instead of a C string.
Definition variable.c:1218
rb_respond_to
int rb_respond_to(VALUE obj, ID mid)
Queries if the object responds to the method.
Definition vm_method.c:2805
rb_check_funcall
VALUE rb_check_funcall(VALUE recv, ID mid, int argc, const VALUE *argv)
Identical to rb_funcallv(), except it returns RUBY_Qundef instead of raising rb_eNoMethodError.
Definition vm_eval.c:664
rb_intern_const
static ID rb_intern_const(const char *str)
This is a "tiny optimisation" over rb_intern().
Definition symbol.h:276
rb_intern
ID rb_intern(const char *name)
Finds or creates a symbol of the given name.
Definition symbol.c:796
rb_sym2str
VALUE rb_sym2str(VALUE id)
Identical to rb_id2str(), except it takes an instance of rb_cSymbol rather than an ID.
Definition symbol.c:943
RB_BLOCK_CALL_FUNC_ARGLIST
#define RB_BLOCK_CALL_FUNC_ARGLIST(yielded_arg, callback_arg)
Shim for block function parameters.
Definition iterator.h:58
rb_yield
VALUE rb_yield(VALUE val)
Yields the block.
Definition vm_eval.c:1357
rb_marshal_define_compat
void rb_marshal_define_compat(VALUE newclass, VALUE oldclass, VALUE(*dumper)(VALUE), VALUE(*loader)(VALUE, VALUE))
Marshal format compatibility layer.
Definition marshal.c:150
ruby::backward::cxxanyargs::rb_block_call
VALUE rb_block_call(VALUE q, ID w, int e, const VALUE *r, type *t, VALUE y)
Call a method with a block.
Definition cxxanyargs.hpp:232
ruby::backward::cxxanyargs::rb_rescue2
VALUE rb_rescue2(type *q, VALUE w, type *e, VALUE r,...)
An equivalent of rescue clause.
Definition cxxanyargs.hpp:284
RBIMPL_ATTR_NORETURN
#define RBIMPL_ATTR_NORETURN()
Wraps (or simulates) [[noreturn]]
Definition noreturn.h:38
RARRAY_AREF
#define RARRAY_AREF(a, i)
Definition rarray.h:583
RBASIC
#define RBASIC(obj)
Convenient casting macro.
Definition rbasic.h:40
RBIGNUM_SIGN
#define RBIGNUM_SIGN
Just another name of rb_big_sign.
Definition rbignum.h:29
RBIGNUM_NEGATIVE_P
static bool RBIGNUM_NEGATIVE_P(VALUE b)
Checks if the bignum is negative.
Definition rbignum.h:74
RBIGNUM_POSITIVE_P
static bool RBIGNUM_POSITIVE_P(VALUE b)
Checks if the bignum is positive.
Definition rbignum.h:61
rb_obj_classname
const char * rb_obj_classname(VALUE obj)
Queries the name of the class of the passed object.
Definition variable.c:325
RTEST
#define RTEST
This is an old name of RB_TEST.
Definition special_consts.h:51
ID
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
Definition value.h:52
VALUE
uintptr_t VALUE
Type that represents a Ruby object.
Definition value.h:40
RB_FLOAT_TYPE_P
static bool RB_FLOAT_TYPE_P(VALUE obj)
Queries if the object is an instance of rb_cFloat.
Definition value_type.h:263
RB_TYPE_P
static bool RB_TYPE_P(VALUE obj, enum ruby_value_type t)
Queries if the given object is of given type.
Definition value_type.h:375