[#29374] nil.to_s — Shugo Maeda <shugo@...>

前田です。

59 messages 2006/09/01
[#29375] Re: nil.to_s — "U.Nakamura" <usa@...> 2006/09/01

こんにちは、なかむら(う)です。

[#29380] Re: nil.to_s — Yukihiro Matsumoto <matz@...> 2006/09/01

まつもと ゆきひろです

[#29387] Re: nil.to_s — Shugo Maeda <shugo@...> 2006/09/01

前田です。

[#29390] Re: nil.to_s — Yukihiro Matsumoto <matz@...> 2006/09/01

まつもと ゆきひろです

[#29398] Re: nil.to_s — "NARUSE, Yui" <naruse@...> 2006/09/01

成瀬です。

[#29400] Re: nil.to_s — Yukihiro Matsumoto <matz@...> 2006/09/01

まつもと ゆきひろです

[#29491] symbol and string — Tanaka Akira <akr@...>

open-uri で :proxy=>nil という指定を行うと、以下のようにエラーになります。

33 messages 2006/09/05
[#29499] Re: symbol and string — Yukihiro Matsumoto <matz@...> 2006/09/05

まつもと ゆきひろです

[#29500] Re: symbol and string — Tanaka Akira <akr@...> 2006/09/05

In article <1157470154.047826.13379.nullmailer@x31.priv.netlab.jp>,

[#29503] Re: symbol and string — Yukihiro Matsumoto <matz@...> 2006/09/06

まつもと ゆきひろです

[#29504] Re: symbol and string — Tanaka Akira <akr@...> 2006/09/06

In article <1157505538.340126.8472.nullmailer@x31.priv.netlab.jp>,

[#29507] Re: symbol and string — Yukihiro Matsumoto <matz@...> 2006/09/06

まつもと ゆきひろです

[#29512] Re: symbol and string — keiju@... (石塚圭樹) 2006/09/06

けいじゅ@いしつかです.

[#29529] Re: symbol and string — SASADA Koichi <ko1@...> 2006/09/08

 ささだです。

[#29530] Re: symbol and string — Yukihiro Matsumoto <matz@...> 2006/09/08

まつもと ゆきひろです

[ruby-dev:29585] Re: nil.to_s

From: Nobuyoshi Nakada <nobu@...>
Date: 2006-09-17 04:54:25 UTC
List: ruby-dev #29585
なかだです。

At Fri, 8 Sep 2006 01:01:06 +0900,
Yukihiro Matsumoto wrote in [ruby-dev:29520]:
>   * Array#to_sとHash#to_sは何を返すべきか
> 
>     Perl由来の今の挙動は嬉しいことがなにひとつない。
>     Pythonのようにinspect(repr)のaliasにすべき(か)。

inspectよりは、to_sしたものを適当なセパレータでjoinというのを希
望します。つまり、inspect同様再帰するが、要素に対してもinspect
ではなくto_sを使うということです。to_sに期待される要素はやはり
to_sされたものではないかと思うので。

こんな感じで。

  class Array
    def to_s(sep = $, || ", ") join(sep) end
  end

  class Hash
    def to_s(keysep = " => ", *rest)
      collect {|elem| elem.to_s(keysep)}.to_s(*rest)
    end
  end


Index: array.c
===================================================================
RCS file: /cvs/ruby/src/ruby/array.c,v
retrieving revision 1.195
diff -p -u -2 -r1.195 array.c
--- array.c	16 Sep 2006 02:24:58 -0000	1.195
+++ array.c	17 Sep 2006 03:14:32 -0000
@@ -1397,5 +1397,4 @@ inspect_ary(VALUE ary, VALUE dummy, int 
 /*
  *  call-seq:
- *     array.to_s -> string
  *     array.inspect  -> string
  *
@@ -1410,8 +1409,29 @@ rb_ary_inspect(VALUE ary)
 }
 
+/*
+ *  call-seq:
+ *     array.to_s(sep=$,||", ") -> string
+ *  
+ *  Returns a string created by converting each element of the array to
+ *  a string, separated by <i>sep</i>.
+ *     
+ *     [ "a", "b", "c" ].to_s        #=> "a, b, c"
+ *
+ *     [ "a", "b", "c" ].to_s("-")   #=> "a-b-c"
+ *
+ *     $, = "/"
+ *     [ "a", "b", "c" ].to_s        #=> "a/b/c"
+ */
+
 VALUE
-rb_ary_to_s(VALUE ary)
+rb_ary_to_s(int argc, VALUE *argv, VALUE ary)
 {
-    return rb_ary_inspect(ary);
+    VALUE sep;
+
+    rb_scan_args(argc, argv, "01", &sep);
+    if (NIL_P(sep)) sep = rb_output_fs;
+    if (NIL_P(sep)) sep = rb_str_new2(", ");
+
+    return rb_ary_join(ary, sep);
 }
 
@@ -2953,5 +2973,5 @@ Init_Array(void)
     rb_define_method(rb_cArray, "initialize_copy", rb_ary_replace, 1);
 
-    rb_define_method(rb_cArray, "to_s", rb_ary_inspect, 0);
+    rb_define_method(rb_cArray, "to_s", rb_ary_to_s, -1);
     rb_define_method(rb_cArray, "inspect", rb_ary_inspect, 0);
     rb_define_method(rb_cArray, "to_a", rb_ary_to_a, 0);
Index: hash.c
===================================================================
RCS file: /cvs/ruby/src/ruby/hash.c,v
retrieving revision 1.170
diff -p -u -2 -r1.170 hash.c
--- hash.c	13 Sep 2006 08:10:28 -0000	1.170
+++ hash.c	17 Sep 2006 03:31:12 -0000
@@ -1123,9 +1123,7 @@ inspect_i(VALUE key, VALUE value, VALUE 
     str2 = rb_inspect(key);
     rb_str_buf_append(str, str2);
-    OBJ_INFECT(str, str2);
     rb_str_buf_cat2(str, "=>");
     str2 = rb_inspect(value);
     rb_str_buf_append(str, str2);
-    OBJ_INFECT(str, str2);
 
     return ST_CONTINUE;
@@ -1148,11 +1146,10 @@ inspect_hash(VALUE hash, VALUE dummy, in
 /*
  * call-seq:
- *   hsh.to_s   => string
  *   hsh.inspect  => string
  *
  * Return the contents of this hash as a string.
- *     
+ *
  *     h = { "c" => 300, "a" => 100, "d" => 400, "c" => 300  }
- *     h.to_s   #=> "{\"a\"=>100, \"c\"=>300, \"d\"=>400}"
+ *     h.inspect   #=> "{\"a\"=>100, \"c\"=>300, \"d\"=>400}"
  */
 
@@ -1165,4 +1162,69 @@ rb_hash_inspect(VALUE hash)
 }
 
+struct hash_to_s_arg {
+    VALUE str, pair, elem;
+};
+
+static int
+to_s_i(VALUE key, VALUE value, VALUE argp)
+{
+    struct hash_to_s_arg *arg = (struct hash_to_s_arg *)argp;
+    VALUE str = arg->str;
+    VALUE pairsep = arg->pair;
+    VALUE elemsep = arg->elem;
+
+    if ((VALUE)key == Qundef) return (int)ST_CONTINUE;
+    if (NIL_P(str)) {
+    }
+    else if (RSTRING_LEN(str) > 1) {
+	if (NIL_P(arg->elem)) arg->elem = rb_str_new2(", ");
+	rb_str_buf_append(str, arg->elem);
+    }
+    rb_str_buf_append(str, rb_obj_as_string((VALUE)key));
+    if (NIL_P(arg->pair)) arg->pair = rb_str_new2("=>");
+    rb_str_buf_append(str, arg->pair);
+    rb_str_buf_append(str, rb_obj_as_string((VALUE)value));
+
+    return ST_CONTINUE;
+}
+
+static VALUE
+to_s_hash(VALUE hash, VALUE argp, int recur)
+{
+    VALUE str = ((struct hash_to_s_arg *)argp)->str;
+
+    if (recur) return rb_str_new2("{...}");
+    rb_hash_foreach(hash, to_s_i, argp);
+    OBJ_INFECT(str, hash);
+
+    return str;
+}
+
+/*
+ * call-seq:
+ *   hsh.to_s(pairsep="=>", elemsep=$,)   => string
+ *
+ * Return the contents of this hash as a string.
+ *     
+ *     h = { "c" => 300, "a" => 100, "d" => 400, "c" => 300  }
+ *     h.to_s   #=> "{a=>100, c=>300, d=>400}"
+ */
+
+static VALUE
+rb_hash_to_s(int argc, VALUE *argv, VALUE hash)
+{
+    struct hash_to_s_arg args;
+
+    args.str = rb_str_new(0, 0);
+    if (RHASH(hash)->tbl == 0 || RHASH(hash)->tbl->num_entries == 0)
+	return args.str;
+
+    rb_scan_args(argc, argv, "02", &args.pair, &args.elem);
+    if (NIL_P(args.elem)) args.elem = rb_output_fs;
+    if (!NIL_P(args.pair)) StringValue(args.pair);
+    if (!NIL_P(args.elem)) StringValue(args.elem);
+    return rb_exec_recursive(to_s_hash, hash, (VALUE)&args);
+}
+
 /*
  * call-seq:
@@ -2326,5 +2388,5 @@ Init_Hash(void)
     rb_define_method(rb_cHash,"to_hash", rb_hash_to_hash, 0);
     rb_define_method(rb_cHash,"to_a", rb_hash_to_a, 0);
-    rb_define_method(rb_cHash,"to_s", rb_hash_inspect, 0);
+    rb_define_method(rb_cHash,"to_s", rb_hash_to_s, -1);
     rb_define_method(rb_cHash,"inspect", rb_hash_inspect, 0);
 
Index: intern.h
===================================================================
RCS file: /cvs/ruby/src/ruby/intern.h,v
retrieving revision 1.200
diff -p -u -2 -r1.200 intern.h
--- intern.h	14 Sep 2006 07:27:14 -0000	1.200
+++ intern.h	17 Sep 2006 03:15:26 -0000
@@ -47,5 +47,5 @@ void rb_ary_store(VALUE, long, VALUE);
 VALUE rb_ary_dup(VALUE);
 VALUE rb_ary_to_ary(VALUE);
-VALUE rb_ary_to_s(VALUE);
+VALUE rb_ary_to_s(int, VALUE*, VALUE);
 VALUE rb_ary_push(VALUE, VALUE);
 VALUE rb_ary_pop(VALUE);


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

In This Thread