summaryrefslogtreecommitdiff
path: root/range.c
diff options
context:
space:
mode:
authormame <mame@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2018-06-22 02:58:37 (GMT)
committermame <mame@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2018-06-22 02:58:37 (GMT)
commitc19ecf05b4c728951e1a1e223a40ae6883a4f8e0 (patch)
treea9c8e11f3540c6852cd9ac1b1327fef67d104a24 /range.c
parent3afb77330fc06d112a5243e5486f043c1339a90b (diff)
range.c: Range#to_a now raises RangeError if it is endless
Fixes [Bug #14845] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@63714 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'range.c')
-rw-r--r--range.c22
1 files changed, 22 insertions, 0 deletions
diff --git a/range.c b/range.c
index dcae2b2..3414525 100644
--- a/range.c
+++ b/range.c
@@ -755,6 +755,26 @@ range_size(VALUE range)
return Qnil;
}
+/*
+ * call-seq:
+ * rng.to_a -> array
+ * rng.entries -> array
+ *
+ * Returns an array containing the items in <i>rng</i>.
+ *
+ * (1..7).to_a #=> [1, 2, 3, 4, 5, 6, 7]
+ * (1..).to_a #=> RangeError: cannot convert endless range to an array
+ */
+
+static VALUE
+range_to_a(VALUE range)
+{
+ if (NIL_P(RANGE_END(range))) {
+ rb_raise(rb_eRangeError, "cannot convert endless range to an array");
+ }
+ return rb_call_super(0, 0);
+}
+
static VALUE
range_enum_size(VALUE range, VALUE args, VALUE eobj)
{
@@ -1459,6 +1479,8 @@ Init_Range(void)
rb_define_method(rb_cRange, "min", range_min, -1);
rb_define_method(rb_cRange, "max", range_max, -1);
rb_define_method(rb_cRange, "size", range_size, 0);
+ rb_define_method(rb_cRange, "to_a", range_to_a, 0);
+ rb_define_method(rb_cRange, "entries", range_to_a, 0);
rb_define_method(rb_cRange, "to_s", range_to_s, 0);
rb_define_method(rb_cRange, "inspect", range_inspect, 0);