[ruby-dev:31758] Re: deprecating RUBY_VERSION_CODE

From: Nobuyoshi Nakada <nobu@...>
Date: 2007-09-07 16:42:50 UTC
List: ruby-dev #31758
なかだです。

At Thu, 6 Sep 2007 13:50:59 +0900,
Nobuyoshi Nakada wrote in [ruby-dev:31743]:
> RUBY_VERSIONをRuby::VersionNumberというクラスのオブジェクトにし
> てみました。最初はto_strを持つだけのStructにしようかと思ったんで
> すが、gsubなどが使われていたので中身はそのままStringにしてしまい
> ました。

to_aryやto_aを定義していると puts RUBY_VERSION でかなり惨敗した
気分なので、to_splatを使うようにしてみました。

しかし、RUBY_VERSIONをどうするかとは別に、このcheck_splat()のよ
うな関数は提供しているとなにかと便利のような気がするのですが。


diff -U2 version.c version.c
--- version.c	(working copy)
+++ version.c	(working copy)
@@ -43,5 +43,5 @@
 
 static VALUE
-version_to_a(VALUE self)
+version_to_splat(VALUE self)
 {
     return rb_ary_new3(3, version_major(self), version_minor(self), version_teeny(self));
@@ -49,12 +49,18 @@
 
 static VALUE
+check_splat(VALUE ary)
+{
+    return rb_check_convert_type(ary, T_ARRAY, "Array", "to_splat");
+}
+
+static VALUE
 version_cmp(VALUE self, VALUE other)
 {
-    VALUE tmp = rb_check_convert_type(other, T_ARRAY, "Array", "to_a");
+    VALUE tmp = check_splat(other);
     long i;
 
     if (NIL_P(tmp)) {
 	VALUE sep = rb_const_get(rb_obj_class(self), rb_intern("Separator"));
-	tmp = rb_ary_to_ary(rb_funcall2(other, rb_intern("split"), 1, &sep));
+	tmp = check_splat(rb_funcall2(other, rb_intern("split"), 1, &sep));
     }
     other = rb_ary_new2(RARRAY_LEN(tmp));
@@ -64,5 +70,5 @@
     }
 
-    self = rb_convert_type(self, T_ARRAY, "Array", "to_a");
+    self = check_splat(self);
     return rb_ary_cmp(self, other);
 }
@@ -74,6 +80,5 @@
 
     rb_define_method(cVer, "initialize", version_init, 3);
-    rb_define_method(cVer, "to_ary", version_to_a, 0);
-    rb_define_method(cVer, "to_a", version_to_a, 0);
+    rb_define_method(cVer, "to_splat", version_to_splat, 0);
     rb_define_method(cVer, "<=>", version_cmp, 1);
     rb_define_method(cVer, "major", version_major, 0);


-- 
--- 僕の前にBugはない。
--- 僕の後ろにBugはできる。
    中田 伸悦

In This Thread

Prev Next