summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2006-10-02 17:39:57 +0000
committermatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2006-10-02 17:39:57 +0000
commit9317700a5dc77053b84180456fa0ae10a3d33218 (patch)
tree127ad25068a7550f404dd9ffb4816206e644b066
parent2c875a0fde1c0b3190b56b7637624ea70b95a434 (diff)
* eval.c (splat_value): use "to_splat" instead of "to_ary" to
prepare splat values as an array. * array.c (Init_Array): define to_splat. * range.c (range_to_splat): new method. * enumerator.c (enumerator_to_splat): ditto. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@11069 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--ChangeLog11
-rw-r--r--array.c1
-rw-r--r--enumerator.c14
-rw-r--r--eval.c19
-rw-r--r--range.c14
5 files changed, 56 insertions, 3 deletions
diff --git a/ChangeLog b/ChangeLog
index d544ad3a40e..c67e702ab36 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,14 @@
+Tue Oct 3 02:31:13 2006 Yukihiro Matsumoto <matz@ruby-lang.org>
+
+ * eval.c (splat_value): use "to_splat" instead of "to_ary" to
+ prepare splat values as an array.
+
+ * array.c (Init_Array): define to_splat.
+
+ * range.c (range_to_splat): new method.
+
+ * enumerator.c (enumerator_to_splat): ditto.
+
Tue Oct 3 01:36:47 2006 Yukihiro Matsumoto <matz@ruby-lang.org>
* string.c (rb_str_lines): returns an Enumerator instead of an
diff --git a/array.c b/array.c
index a60df5d4321..a833e54157d 100644
--- a/array.c
+++ b/array.c
@@ -3017,6 +3017,7 @@ Init_Array(void)
rb_define_method(rb_cArray, "to_s", rb_ary_inspect, 0);
rb_define_method(rb_cArray, "inspect", rb_ary_inspect, 0);
rb_define_method(rb_cArray, "to_a", rb_ary_to_a, 0);
+ rb_define_method(rb_cArray, "to_splat", rb_ary_to_a, 0);
rb_define_method(rb_cArray, "to_ary", rb_ary_to_ary_m, 0);
rb_define_method(rb_cArray, "frozen?", rb_ary_frozen_p, 0);
diff --git a/enumerator.c b/enumerator.c
index ec348ded372..727990f652d 100644
--- a/enumerator.c
+++ b/enumerator.c
@@ -359,6 +359,19 @@ enumerator_with_index(VALUE obj)
enumerator_with_index_i, (VALUE)&memo);
}
+/*
+ * call-seq:
+ * e.to_splat => array
+ *
+ * Convert this enumerator object to an array to splat.
+ */
+
+static VALUE
+enumerator_to_splat(VALUE range)
+{
+ return rb_convert_type(range, T_ARRAY, "Array", "to_a");
+}
+
void
Init_Enumerator(void)
{
@@ -378,6 +391,7 @@ Init_Enumerator(void)
rb_define_method(rb_cEnumerator, "initialize", enumerator_initialize, -1);
rb_define_method(rb_cEnumerator, "each", enumerator_each, 0);
rb_define_method(rb_cEnumerator, "with_index", enumerator_with_index, 0);
+ rb_define_method(rb_cEnumerator, "to_splat", enumerator_to_splat, 0);
sym_each = ID2SYM(rb_intern("each"));
sym_each_with_index = ID2SYM(rb_intern("each_with_index"));
diff --git a/eval.c b/eval.c
index 1ef5143f876..60deac375ae 100644
--- a/eval.c
+++ b/eval.c
@@ -2620,6 +2620,19 @@ svalue_to_avalue(VALUE v)
}
static VALUE
+splat_value(VALUE v)
+{
+ VALUE tmp;
+
+ if (v == Qundef) return rb_ary_new2(0);
+ tmp = rb_check_convert_type(v, T_ARRAY, "Array", "to_splat");
+ if (NIL_P(tmp)) {
+ return rb_ary_new3(1, v);
+ }
+ return tmp;
+}
+
+static VALUE
class_prefix(VALUE self, NODE *cpath)
{
if (!cpath) {
@@ -2703,7 +2716,7 @@ when_check(NODE *tag, VALUE val, VALUE self)
}
break;
case NODE_SPLAT:
- elm = svalue_to_avalue(rb_eval(self, tag->nd_head));
+ elm = splat_value(rb_eval(self, tag->nd_head));
for (i=0; i<RARRAY_LEN(elm); i++) {
if (when_cond(val, RARRAY_PTR(elm)[i])) {
return Qtrue;
@@ -2976,7 +2989,7 @@ rb_eval(VALUE self, NODE *n)
break;
case NODE_SPLAT:
- result = svalue_to_avalue(rb_eval(self, node->nd_head));
+ result = splat_value(rb_eval(self, node->nd_head));
break;
case NODE_TO_ARY:
@@ -3137,7 +3150,7 @@ rb_eval(VALUE self, NODE *n)
case NODE_ARGSCAT:
{
VALUE args = rb_eval(self, node->nd_head);
- result = rb_ary_concat(args, svalue_to_avalue(rb_eval(self, node->nd_body)));
+ result = rb_ary_concat(args, splat_value(rb_eval(self, node->nd_body)));
}
break;
diff --git a/range.c b/range.c
index a7f8e44b37a..8d5bae8179d 100644
--- a/range.c
+++ b/range.c
@@ -571,6 +571,19 @@ range_to_s(VALUE range)
/*
* call-seq:
+ * rng.to_splat => array
+ *
+ * Convert this range object to an array to splat.
+ */
+
+static VALUE
+range_to_splat(VALUE range)
+{
+ return rb_convert_type(range, T_ARRAY, "Array", "to_a");
+}
+
+/*
+ * call-seq:
* rng.inspect => string
*
* Convert this range object to a printable form (using
@@ -746,6 +759,7 @@ Init_Range(void)
rb_define_method(rb_cRange, "max", range_max, 0);
rb_define_method(rb_cRange, "to_s", range_to_s, 0);
rb_define_method(rb_cRange, "inspect", range_inspect, 0);
+ rb_define_method(rb_cRange, "to_splat", range_to_splat, 0);
rb_define_method(rb_cRange, "exclude_end?", range_exclude_end_p, 0);