From: Kazuhiro Yoshida Date: 2003-07-23T19:33:54+09:00 Subject: [ruby-list:37961] Re: "".split level もりきゅうです。 むしろ p "".split(/\t/,1) # [""] がおかしくて [] になるべき… matz@ruby-lang.org (Yukihiro Matsumoto) wrote: > 結果を見るとむしろ > > ruby -e '"".split("\t", 1) > > が[""]を返す方が変で、Perlとの類推から考えるとこれも[]になる > べきではないかと考えます。 ですか。 Perlで書いてみた: D:\usr\moriq>type split0.pl use strict; #print "<", ((defined "")?"T":"F"), ">\n"; #print "<", ((defined undef)?"T":"F"), ">\n"; sub str_inspect { my ($str) = @_; (defined $str)?qq{"$str"}:"nil"; } sub strp { my ($str) = @_; print str_inspect($str), "\n"; } sub aryp { print "[", join(', ', map {str_inspect $_} @_), "]\n"; } #aryp ("a",undef,"b"); #aryp ("a","b",undef); sub test { my ($str) = @_; print "--- str="; strp $str; aryp split(/x/, $str); my $t = sub { my ($limit) = @_; aryp split(/x/, $str, $limit); }; $t->(0); $t->(1); $t->(2); $t->(3); $t->(-1); } test(""); test("x"); test("axbxc"); D:\usr\moriq>perl split0.pl --- str="" [] [] [] [] [] [] --- str="x" [] [] ["x"] ["", ""] ["", ""] ["", ""] --- str="axbxc" ["a", "b", "c"] ["a", "b", "c"] ["axbxc"] ["a", "bxc"] ["a", "b", "c"] ["a", "b", "c"] D:\usr\moriq>perl -v This is perl, v5.8.0 built for MSWin32-x86-multi-thread ... D:\usr\moriq>type split0.rb def test(str) print "--- str=" p str p str.split(/x/) t = proc {|limit| p str.split(/x/,limit) } t.call(0) t.call(1) t.call(2) t.call(3) t.call(-1) end test("") test("x") test("axbxc") D:\usr\moriq>d:\usr\bin\ruby split0.rb --- str="" [] [] [""] [] [] [] --- str="x" [] [] ["x"] ["", ""] ["", ""] ["", ""] --- str="axbxc" ["a", "b", "c"] ["a", "b", "c"] ["axbxc"] ["a", "bxc"] ["a", "b", "c"] ["a", "b", "c"] D:\usr\moriq>d:\usr\bin\ruby -v ruby 1.8.0 (2003-06-23) [i386-mswin32] ---- YOSHIDA Kazuhiro moriq@moriq.com http://www.moriq.com/