* io.c (rb_open_file): encoding in mode string was ignored if perm is
[ruby-svn.git] / range.c
blobbbf98305538bc17dafe9ea735c9e3d02d5162582
1 /**********************************************************************
3 range.c -
5 $Author$
6 created at: Thu Aug 19 17:46:47 JST 1993
8 Copyright (C) 1993-2007 Yukihiro Matsumoto
10 **********************************************************************/
12 #include "ruby/ruby.h"
13 #include "ruby/encoding.h"
15 VALUE rb_cRange;
16 static ID id_cmp, id_succ, id_beg, id_end, id_excl;
18 #define RANGE_BEG(r) (RSTRUCT(r)->as.ary[0])
19 #define RANGE_END(r) (RSTRUCT(r)->as.ary[1])
20 #define RANGE_EXCL(r) (RSTRUCT(r)->as.ary[2])
22 #define EXCL(r) RTEST(RANGE_EXCL(r))
23 #define SET_EXCL(r,v) (RSTRUCT(r)->as.ary[2] = (v) ? Qtrue : Qfalse)
25 static VALUE
26 range_failed(void)
28 rb_raise(rb_eArgError, "bad value for range");
29 return Qnil; /* dummy */
32 static VALUE
33 range_check(VALUE *args)
35 return rb_funcall(args[0], id_cmp, 1, args[1]);
38 static void
39 range_init(VALUE range, VALUE beg, VALUE end, int exclude_end)
41 VALUE args[2];
43 args[0] = beg;
44 args[1] = end;
46 if (!FIXNUM_P(beg) || !FIXNUM_P(end)) {
47 VALUE v;
49 v = rb_rescue(range_check, (VALUE)args, range_failed, 0);
50 if (NIL_P(v))
51 range_failed();
54 SET_EXCL(range, exclude_end);
55 RSTRUCT(range)->as.ary[0] = beg;
56 RSTRUCT(range)->as.ary[1] = end;
59 VALUE
60 rb_range_new(VALUE beg, VALUE end, int exclude_end)
62 VALUE range = rb_obj_alloc(rb_cRange);
64 range_init(range, beg, end, exclude_end);
65 return range;
69 * call-seq:
70 * Range.new(start, end, exclusive=false) => range
72 * Constructs a range using the given <i>start</i> and <i>end</i>. If the third
73 * parameter is omitted or is <code>false</code>, the <i>range</i> will include
74 * the end object; otherwise, it will be excluded.
77 static VALUE
78 range_initialize(int argc, VALUE *argv, VALUE range)
80 VALUE beg, end, flags;
82 rb_scan_args(argc, argv, "21", &beg, &end, &flags);
83 /* Ranges are immutable, so that they should be initialized only once. */
84 if (RANGE_EXCL(range) != Qnil) {
85 rb_name_error(rb_intern("initialize"), "`initialize' called twice");
87 range_init(range, beg, end, RTEST(flags));
88 return Qnil;
93 * call-seq:
94 * rng.exclude_end? => true or false
96 * Returns <code>true</code> if <i>rng</i> excludes its end value.
99 static VALUE
100 range_exclude_end_p(VALUE range)
102 return EXCL(range) ? Qtrue : Qfalse;
107 * call-seq:
108 * rng == obj => true or false
110 * Returns <code>true</code> only if <i>obj</i> is a Range, has equivalent
111 * beginning and end items (by comparing them with <code>==</code>), and has
112 * the same #exclude_end? setting as <i>rng</t>.
114 * (0..2) == (0..2) #=> true
115 * (0..2) == Range.new(0,2) #=> true
116 * (0..2) == (0...2) #=> false
120 static VALUE
121 range_eq(VALUE range, VALUE obj)
123 if (range == obj)
124 return Qtrue;
125 if (!rb_obj_is_instance_of(obj, rb_obj_class(range)))
126 return Qfalse;
128 if (!rb_equal(RANGE_BEG(range), RANGE_BEG(obj)))
129 return Qfalse;
130 if (!rb_equal(RANGE_END(range), RANGE_END(obj)))
131 return Qfalse;
133 if (EXCL(range) != EXCL(obj))
134 return Qfalse;
136 return Qtrue;
139 static int
140 r_lt(VALUE a, VALUE b)
142 VALUE r = rb_funcall(a, id_cmp, 1, b);
144 if (NIL_P(r))
145 return Qfalse;
146 if (rb_cmpint(r, a, b) < 0)
147 return Qtrue;
148 return Qfalse;
151 static int
152 r_le(VALUE a, VALUE b)
154 int c;
155 VALUE r = rb_funcall(a, id_cmp, 1, b);
157 if (NIL_P(r))
158 return Qfalse;
159 c = rb_cmpint(r, a, b);
160 if (c == 0)
161 return INT2FIX(0);
162 if (c < 0)
163 return Qtrue;
164 return Qfalse;
169 * call-seq:
170 * rng.eql?(obj) => true or false
172 * Returns <code>true</code> only if <i>obj</i> is a Range, has equivalent
173 * beginning and end items (by comparing them with #eql?), and has the same
174 * #exclude_end? setting as <i>rng</i>.
176 * (0..2) == (0..2) #=> true
177 * (0..2) == Range.new(0,2) #=> true
178 * (0..2) == (0...2) #=> false
182 static VALUE
183 range_eql(VALUE range, VALUE obj)
185 if (range == obj)
186 return Qtrue;
187 if (!rb_obj_is_instance_of(obj, rb_obj_class(range)))
188 return Qfalse;
190 if (!rb_eql(RANGE_BEG(range), RANGE_BEG(obj)))
191 return Qfalse;
192 if (!rb_eql(RANGE_END(range), RANGE_END(obj)))
193 return Qfalse;
195 if (EXCL(range) != EXCL(obj))
196 return Qfalse;
198 return Qtrue;
202 * call-seq:
203 * rng.hash => fixnum
205 * Generate a hash value such that two ranges with the same start and
206 * end points, and the same value for the "exclude end" flag, generate
207 * the same hash value.
210 static VALUE
211 range_hash(VALUE range)
213 long hash = EXCL(range);
214 VALUE v;
216 v = rb_hash(RANGE_BEG(range));
217 hash ^= v << 1;
218 v = rb_hash(RANGE_END(range));
219 hash ^= v << 9;
220 hash ^= EXCL(range) << 24;
222 return LONG2FIX(hash);
225 static void
226 range_each_func(VALUE range, VALUE (*func) (VALUE, void *), void *arg)
228 int c;
229 VALUE b = RANGE_BEG(range);
230 VALUE e = RANGE_END(range);
231 VALUE v = b;
233 if (EXCL(range)) {
234 while (r_lt(v, e)) {
235 (*func) (v, arg);
236 v = rb_funcall(v, id_succ, 0, 0);
239 else {
240 while (RTEST(c = r_le(v, e))) {
241 (*func) (v, arg);
242 if (c == INT2FIX(0))
243 break;
244 v = rb_funcall(v, id_succ, 0, 0);
249 static VALUE
250 step_i(VALUE i, void *arg)
252 VALUE *iter = arg;
254 if (FIXNUM_P(iter[0])) {
255 iter[0] -= INT2FIX(1) & ~FIXNUM_FLAG;
257 else {
258 iter[0] = rb_funcall(iter[0], '-', 1, INT2FIX(1));
260 if (iter[0] == INT2FIX(0)) {
261 rb_yield(i);
262 iter[0] = iter[1];
264 return Qnil;
268 * call-seq:
269 * rng.step(n=1) {| obj | block } => rng
271 * Iterates over <i>rng</i>, passing each <i>n</i>th element to the block. If
272 * the range contains numbers, <i>n</i> is added for each iteration. Otherwise
273 * <code>step</code> invokes <code>succ</code> to iterate through range
274 * elements. The following code uses class <code>Xs</code>, which is defined
275 * in the class-level documentation.
277 * range = Xs.new(1)..Xs.new(10)
278 * range.step(2) {|x| puts x}
279 * range.step(3) {|x| puts x}
281 * <em>produces:</em>
283 * 1 x
284 * 3 xxx
285 * 5 xxxxx
286 * 7 xxxxxxx
287 * 9 xxxxxxxxx
288 * 1 x
289 * 4 xxxx
290 * 7 xxxxxxx
291 * 10 xxxxxxxxxx
295 static VALUE
296 range_step(int argc, VALUE *argv, VALUE range)
298 VALUE b, e, step, tmp;
300 RETURN_ENUMERATOR(range, argc, argv);
302 b = RANGE_BEG(range);
303 e = RANGE_END(range);
304 if (argc == 0) {
305 step = INT2FIX(1);
307 else {
308 rb_scan_args(argc, argv, "01", &step);
309 if (!rb_obj_is_kind_of(step, rb_cNumeric)) {
310 step = rb_to_int(step);
312 if (rb_funcall(step, '<', 1, INT2FIX(0))) {
313 rb_raise(rb_eArgError, "step can't be negative");
315 else if (!rb_funcall(step, '>', 1, INT2FIX(0))) {
316 rb_raise(rb_eArgError, "step can't be 0");
320 if (FIXNUM_P(b) && FIXNUM_P(e) && FIXNUM_P(step)) { /* fixnums are special */
321 long end = FIX2LONG(e);
322 long i, unit = FIX2LONG(step);
324 if (!EXCL(range))
325 end += 1;
326 i = FIX2LONG(b);
327 while (i < end) {
328 rb_yield(LONG2NUM(i));
329 if (i + unit < i) break;
330 i += unit;
334 else if (rb_obj_is_kind_of(b, rb_cNumeric) ||
335 !NIL_P(rb_check_to_integer(b, "to_int")) ||
336 !NIL_P(rb_check_to_integer(e, "to_int"))) {
337 ID op = EXCL(range) ? '<' : rb_intern("<=");
339 while (RTEST(rb_funcall(b, op, 1, e))) {
340 rb_yield(b);
341 b = rb_funcall(b, '+', 1, step);
344 else {
345 tmp = rb_check_string_type(b);
347 if (!NIL_P(tmp)) {
348 VALUE args[2], iter[2];
350 b = tmp;
351 args[0] = e;
352 args[1] = EXCL(range) ? Qtrue : Qfalse;
353 iter[0] = INT2FIX(1);
354 iter[1] = step;
355 rb_block_call(b, rb_intern("upto"), 2, args, step_i, (VALUE)iter);
357 else {
358 VALUE args[2];
360 if (!rb_respond_to(b, id_succ)) {
361 rb_raise(rb_eTypeError, "can't iterate from %s",
362 rb_obj_classname(b));
364 args[0] = INT2FIX(1);
365 args[1] = step;
366 range_each_func(range, step_i, args);
369 return range;
372 static VALUE
373 each_i(VALUE v, void *arg)
375 rb_yield(v);
376 return Qnil;
380 * call-seq:
381 * rng.each {| i | block } => rng
383 * Iterates over the elements <i>rng</i>, passing each in turn to the
384 * block. You can only iterate if the start object of the range
385 * supports the +succ+ method (which means that you can't iterate over
386 * ranges of +Float+ objects).
388 * (10..15).each do |n|
389 * print n, ' '
390 * end
392 * <em>produces:</em>
394 * 10 11 12 13 14 15
397 static VALUE
398 range_each(VALUE range)
400 VALUE beg, end;
402 RETURN_ENUMERATOR(range, 0, 0);
404 beg = RANGE_BEG(range);
405 end = RANGE_END(range);
407 if (!rb_respond_to(beg, id_succ)) {
408 rb_raise(rb_eTypeError, "can't iterate from %s",
409 rb_obj_classname(beg));
411 if (FIXNUM_P(beg) && FIXNUM_P(end)) { /* fixnums are special */
412 long lim = FIX2LONG(end);
413 long i;
415 if (!EXCL(range))
416 lim += 1;
417 for (i = FIX2LONG(beg); i < lim; i++) {
418 rb_yield(LONG2FIX(i));
421 else if (TYPE(beg) == T_STRING) {
422 VALUE args[2];
424 args[0] = end;
425 args[1] = EXCL(range) ? Qtrue : Qfalse;
426 rb_block_call(beg, rb_intern("upto"), 2, args, rb_yield, 0);
428 else {
429 range_each_func(range, each_i, NULL);
431 return range;
435 * call-seq:
436 * rng.begin => obj
438 * Returns the first object in <i>rng</i>.
441 static VALUE
442 range_begin(VALUE range)
444 return RANGE_BEG(range);
449 * call-seq:
450 * rng.end => obj
452 * Returns the object that defines the end of <i>rng</i>.
454 * (1..10).end #=> 10
455 * (1...10).end #=> 10
459 static VALUE
460 range_end(VALUE range)
462 return RANGE_END(range);
466 static VALUE
467 first_i(VALUE i, VALUE *ary)
469 long n = NUM2LONG(ary[0]);
471 if (n <= 0) {
472 rb_iter_break();
474 rb_ary_push(ary[1], i);
475 n--;
476 ary[0] = INT2NUM(n);
477 return Qnil;
481 * call-seq:
482 * rng.first => obj
483 * rng.first(n) => an_array
485 * Returns the first object in <i>rng</i>, or the first +n+ elements.
488 static VALUE
489 range_first(int argc, VALUE *argv, VALUE range)
491 VALUE n, ary[2];
493 if (argc == 0) return RANGE_BEG(range);
495 rb_scan_args(argc, argv, "1", &n);
496 ary[0] = n;
497 ary[1] = rb_ary_new2(NUM2LONG(n));
498 rb_block_call(range, rb_intern("each"), 0, 0, first_i, (VALUE)ary);
500 return ary[1];
505 * call-seq:
506 * rng.last => obj
507 * rng.last(n) => an_array
509 * Returns the last object in <i>rng</i>, or the last +n+ elements.
512 static VALUE
513 range_last(int argc, VALUE *argv, VALUE range)
515 VALUE rb_ary_last(int, VALUE *, VALUE);
517 if (argc == 0) return RANGE_END(range);
518 return rb_ary_last(argc, argv, rb_Array(range));
523 * call-seq:
524 * rng.min => obj
525 * rng.min {| a,b | block } => obj
527 * Returns the minimum value in <i>rng</i>. The second uses
528 * the block to compare values. Returns nil if the first
529 * value in range is larger than the last value.
534 static VALUE
535 range_min(VALUE range)
537 if (rb_block_given_p()) {
538 return rb_call_super(0, 0);
540 else {
541 VALUE b = RANGE_BEG(range);
542 VALUE e = RANGE_END(range);
543 int c = rb_cmpint(rb_funcall(b, id_cmp, 1, e), b, e);
545 if (c > 0 || (c == 0 && EXCL(range)))
546 return Qnil;
547 return b;
552 * call-seq:
553 * rng.max => obj
554 * rng.max {| a,b | block } => obj
556 * Returns the maximum value in <i>rng</i>. The second uses
557 * the block to compare values. Returns nil if the first
558 * value in range is larger than the last value.
563 static VALUE
564 range_max(VALUE range)
566 VALUE e = RANGE_END(range);
567 int ip = FIXNUM_P(e) || rb_obj_is_kind_of(e, rb_cInteger);
569 if (rb_block_given_p() || (EXCL(range) && !ip)) {
570 return rb_call_super(0, 0);
572 else {
573 VALUE b = RANGE_BEG(range);
574 int c = rb_cmpint(rb_funcall(b, id_cmp, 1, e), b, e);
576 if (c > 0)
577 return Qnil;
578 if (EXCL(range)) {
579 if (c == 0) return Qnil;
580 if (FIXNUM_P(e)) {
581 return LONG2NUM(FIX2LONG(e) - 1);
583 return rb_funcall(e, '-', 1, INT2FIX(1));
585 return e;
589 VALUE
590 rb_range_beg_len(VALUE range, long *begp, long *lenp, long len, int err)
592 VALUE b, e;
593 long beg, end, excl;
595 if (rb_obj_is_kind_of(range, rb_cRange)) {
596 b = RANGE_BEG(range);
597 e = RANGE_END(range);
598 excl = EXCL(range);
600 else {
601 if (!rb_respond_to(range, id_beg)) return Qfalse;
602 if (!rb_respond_to(range, id_end)) return Qfalse;
603 b = rb_funcall(range, id_beg, 0);
604 e = rb_funcall(range, id_end, 0);
605 excl = RTEST(rb_funcall(range, rb_intern("exclude_end?"), 0));
607 beg = NUM2LONG(b);
608 end = NUM2LONG(e);
610 if (beg < 0) {
611 beg += len;
612 if (beg < 0)
613 goto out_of_range;
615 if (err == 0 || err == 2) {
616 if (beg > len)
617 goto out_of_range;
618 if (end > len)
619 end = len;
621 if (end < 0)
622 end += len;
623 if (!excl)
624 end++; /* include end point */
625 len = end - beg;
626 if (len < 0)
627 len = 0;
629 *begp = beg;
630 *lenp = len;
631 return Qtrue;
633 out_of_range:
634 if (err) {
635 rb_raise(rb_eRangeError, "%ld..%s%ld out of range",
636 b, excl ? "." : "", e);
638 return Qnil;
642 * call-seq:
643 * rng.to_s => string
645 * Convert this range object to a printable form.
648 static VALUE
649 range_to_s(VALUE range)
651 VALUE str, str2;
653 str = rb_obj_as_string(RANGE_BEG(range));
654 str2 = rb_obj_as_string(RANGE_END(range));
655 str = rb_str_dup(str);
656 rb_str_cat(str, "...", EXCL(range) ? 3 : 2);
657 rb_str_append(str, str2);
658 OBJ_INFECT(str, str2);
660 return str;
664 * call-seq:
665 * rng.inspect => string
667 * Convert this range object to a printable form (using
668 * <code>inspect</code> to convert the start and end
669 * objects).
673 static VALUE
674 range_inspect(VALUE range)
676 VALUE str, str2;
678 str = rb_inspect(RANGE_BEG(range));
679 str2 = rb_inspect(RANGE_END(range));
680 str = rb_str_dup(str);
681 rb_str_cat(str, "...", EXCL(range) ? 3 : 2);
682 rb_str_append(str, str2);
683 OBJ_INFECT(str, str2);
685 return str;
689 * call-seq:
690 * rng === obj => true or false
692 * Returns <code>true</code> if <i>obj</i> is an element of
693 * <i>rng</i>, <code>false</code> otherwise. Conveniently,
694 * <code>===</code> is the comparison operator used by
695 * <code>case</code> statements.
697 * case 79
698 * when 1..50 then print "low\n"
699 * when 51..75 then print "medium\n"
700 * when 76..100 then print "high\n"
701 * end
703 * <em>produces:</em>
705 * high
708 static VALUE
709 range_eqq(VALUE range, VALUE val)
711 return rb_funcall(range, rb_intern("include?"), 1, val);
716 * call-seq:
717 * rng.member?(val) => true or false
718 * rng.include?(val) => true or false
720 * Returns <code>true</code> if <i>obj</i> is an element of
721 * <i>rng</i>, <code>false</code> otherwise. If beg and end are
722 * numeric, comparison is done according magnitude of values.
724 * ("a".."z").include?("g") # => true
725 * ("a".."z").include?("A") # => false
728 static VALUE
729 range_include(VALUE range, VALUE val)
731 VALUE beg = RANGE_BEG(range);
732 VALUE end = RANGE_END(range);
733 int nv = FIXNUM_P(beg) || FIXNUM_P(end) ||
734 rb_obj_is_kind_of(beg, rb_cNumeric) ||
735 rb_obj_is_kind_of(end, rb_cNumeric);
737 if (nv ||
738 !NIL_P(rb_check_to_integer(beg, "to_int")) ||
739 !NIL_P(rb_check_to_integer(end, "to_int"))) {
740 if (r_le(beg, val)) {
741 if (EXCL(range)) {
742 if (r_lt(val, end))
743 return Qtrue;
745 else {
746 if (r_le(val, end))
747 return Qtrue;
750 return Qfalse;
752 else if (TYPE(beg) == T_STRING && TYPE(end) == T_STRING &&
753 RSTRING_LEN(beg) == 1 && RSTRING_LEN(end) == 1) {
754 if (NIL_P(val)) return Qfalse;
755 if (TYPE(val) == T_STRING) {
756 if (RSTRING_LEN(val) == 0 || RSTRING_LEN(val) > 1)
757 return Qfalse;
758 else {
759 char b = RSTRING_PTR(beg)[0];
760 char e = RSTRING_PTR(end)[0];
761 char v = RSTRING_PTR(val)[0];
763 if (ISASCII(b) && ISASCII(e) && ISASCII(v)) {
764 if (b <= v && v < e) return Qtrue;
765 if (!EXCL(range) && v == e) return Qtrue;
766 return Qfalse;
771 /* TODO: ruby_frame->this_func = rb_intern("include?"); */
772 return rb_call_super(1, &val);
777 * call-seq:
778 * rng.cover?(val) => true or false
780 * Returns <code>true</code> if <i>obj</i> is between beg and end,
781 * i.e <code>beg <= obj <= end</code> (or <i>end</i> exclusive when
782 * <code>exclude_end?</code> is true).
784 * ("a".."z").cover?("c") #=> true
785 * ("a".."z").cover?("5") #=> false
788 static VALUE
789 range_cover(VALUE range, VALUE val)
791 VALUE beg, end;
793 beg = RANGE_BEG(range);
794 end = RANGE_END(range);
795 if (r_le(beg, val)) {
796 if (EXCL(range)) {
797 if (r_lt(val, end))
798 return Qtrue;
800 else {
801 if (r_le(val, end))
802 return Qtrue;
805 return Qfalse;
808 static VALUE
809 range_dumper(VALUE range)
811 VALUE v;
812 NEWOBJ(m, struct RObject);
813 OBJSETUP(m, rb_cObject, T_OBJECT);
815 v = (VALUE)m;
817 rb_ivar_set(v, id_excl, RANGE_EXCL(range));
818 rb_ivar_set(v, id_beg, RANGE_BEG(range));
819 rb_ivar_set(v, id_end, RANGE_END(range));
820 return v;
823 static VALUE
824 range_loader(VALUE range, VALUE obj)
826 if (TYPE(obj) != T_OBJECT || RBASIC(obj)->klass != rb_cObject) {
827 rb_raise(rb_eTypeError, "not a dumped range object");
830 RSTRUCT(range)->as.ary[0] = rb_ivar_get(obj, id_beg);
831 RSTRUCT(range)->as.ary[1] = rb_ivar_get(obj, id_end);
832 RSTRUCT(range)->as.ary[2] = rb_ivar_get(obj, id_excl);
833 return range;
836 static VALUE
837 range_alloc(VALUE klass)
839 /* rb_struct_alloc_noinit itself should not be used because
840 * rb_marshal_define_compat uses equality of allocaiton function */
841 return rb_struct_alloc_noinit(klass);
844 /* A <code>Range</code> represents an interval---a set of values with a
845 * start and an end. Ranges may be constructed using the
846 * <em>s</em><code>..</code><em>e</em> and
847 * <em>s</em><code>...</code><em>e</em> literals, or with
848 * <code>Range::new</code>. Ranges constructed using <code>..</code>
849 * run from the start to the end inclusively. Those created using
850 * <code>...</code> exclude the end value. When used as an iterator,
851 * ranges return each value in the sequence.
853 * (-1..-5).to_a #=> []
854 * (-5..-1).to_a #=> [-5, -4, -3, -2, -1]
855 * ('a'..'e').to_a #=> ["a", "b", "c", "d", "e"]
856 * ('a'...'e').to_a #=> ["a", "b", "c", "d"]
858 * Ranges can be constructed using objects of any type, as long as the
859 * objects can be compared using their <code><=></code> operator and
860 * they support the <code>succ</code> method to return the next object
861 * in sequence.
863 * class Xs # represent a string of 'x's
864 * include Comparable
865 * attr :length
866 * def initialize(n)
867 * @length = n
868 * end
869 * def succ
870 * Xs.new(@length + 1)
871 * end
872 * def <=>(other)
873 * @length <=> other.length
874 * end
875 * def to_s
876 * sprintf "%2d #{inspect}", @length
877 * end
878 * def inspect
879 * 'x' * @length
880 * end
881 * end
883 * r = Xs.new(3)..Xs.new(6) #=> xxx..xxxxxx
884 * r.to_a #=> [xxx, xxxx, xxxxx, xxxxxx]
885 * r.member?(Xs.new(5)) #=> true
887 * In the previous code example, class <code>Xs</code> includes the
888 * <code>Comparable</code> module. This is because
889 * <code>Enumerable#member?</code> checks for equality using
890 * <code>==</code>. Including <code>Comparable</code> ensures that the
891 * <code>==</code> method is defined in terms of the <code><=></code>
892 * method implemented in <code>Xs</code>.
896 void
897 Init_Range(void)
899 #undef rb_intern
900 #define rb_intern(str) rb_intern_const(str)
902 id_cmp = rb_intern("<=>");
903 id_succ = rb_intern("succ");
904 id_beg = rb_intern("begin");
905 id_end = rb_intern("end");
906 id_excl = rb_intern("excl");
908 rb_cRange = rb_struct_define_without_accessor(
909 "Range", rb_cObject, range_alloc,
910 "begin", "end", "excl", NULL);
912 rb_include_module(rb_cRange, rb_mEnumerable);
913 rb_marshal_define_compat(rb_cRange, rb_cObject, range_dumper, range_loader);
914 rb_define_method(rb_cRange, "initialize", range_initialize, -1);
915 rb_define_method(rb_cRange, "==", range_eq, 1);
916 rb_define_method(rb_cRange, "===", range_eqq, 1);
917 rb_define_method(rb_cRange, "eql?", range_eql, 1);
918 rb_define_method(rb_cRange, "hash", range_hash, 0);
919 rb_define_method(rb_cRange, "each", range_each, 0);
920 rb_define_method(rb_cRange, "step", range_step, -1);
921 rb_define_method(rb_cRange, "begin", range_begin, 0);
922 rb_define_method(rb_cRange, "end", range_end, 0);
923 rb_define_method(rb_cRange, "first", range_first, -1);
924 rb_define_method(rb_cRange, "last", range_last, -1);
925 rb_define_method(rb_cRange, "min", range_min, 0);
926 rb_define_method(rb_cRange, "max", range_max, 0);
927 rb_define_method(rb_cRange, "to_s", range_to_s, 0);
928 rb_define_method(rb_cRange, "inspect", range_inspect, 0);
930 rb_define_method(rb_cRange, "exclude_end?", range_exclude_end_p, 0);
932 rb_define_method(rb_cRange, "member?", range_include, 1);
933 rb_define_method(rb_cRange, "include?", range_include, 1);
934 rb_define_method(rb_cRange, "cover?", range_cover, 1);