[ruby-dev:19290] Re: ruby-1.8.0 / yield(nil)とyield()のちがい
From:
matz@... (Yukihiro Matsumoto)
Date:
2003-01-02 15:49:27 UTC
List:
ruby-dev #19290
まつもと ゆきひろです
In message "[ruby-dev:19278] Re: ruby-1.8.0 / yield(nil)とyield()のちがい"
on 03/01/01, Tanaka Akira <akr@m17n.org> writes:
|(む。return *[] は [] を返して、return *[nil] は [nil] を返すけど、こ
|れは意図されているのかなぁ。)
いいえ、return(とbreak)はまだチェックが終わってないのです。
|それはそれとして、もし可能なら、*a のほうがいいと思います。*a のほうが
|直観的だと思うので。
ふうむ。理由を含めて納得できるものがあります。
|> こうすると、その延長線上として
|>
|> *a = [1,2]
|>
|> が[[1,2]]となることとなり、結果として「配列でエミュレートさ
|> れている多値戻り値」が使えなくなると思います。ちゃんとした多
|> 値を導入するとかいう話もありえないではないですが。
|
|というところなわけですが、返り値も多値にしてしまう(つまりちゃんとした
|多値を導入する)か、あるいはそうしなくても
|
| *a = *[1,2]
|
|って書けばいいからそれほど使い勝手は変わらないんじゃないか、と思ってい
|ます。
これだとcgi.rbを使っているプログラムが全滅するので a, = [1]
は a = 1 でなければなりません。
ちゅーわけで、こんな感じかなあ。まだ、検証中ですが。
---
def test_ok(a, b)
if a != b
print "failed at ", caller(0)[1], "<a:", a.inspect, "> != <b:", b.inspect, ">\n"
end
end
a = nil; test_ok(a, nil)
a = 1; test_ok(a, 1)
a = []; test_ok(a, [])
a = [1]; test_ok(a, [1])
a = [nil]; test_ok(a, [nil])
a = [[]]; test_ok(a, [[]])
a = [1,2]; test_ok(a, [1,2])
a = [*[]]; test_ok(a, [])
a = [*[1]]; test_ok(a, [1])
a = [*[1,2]]; test_ok(a, [1,2])
a = *nil; test_ok(a, nil)
a = *1; test_ok(a, 1)
a = *[]; test_ok(a, nil)
a = *[1]; test_ok(a, 1)
a = *[nil]; test_ok(a, nil)
a = *[[]]; test_ok(a, [])
a = *[1,2]; test_ok(a, [1,2])
a = *[*[]]; test_ok(a, nil)
a = *[*[1]]; test_ok(a, 1)
a = *[*[1,2]]; test_ok(a, [1,2])
*a = nil; test_ok(a, [nil])
*a = 1; test_ok(a, [1])
*a = []; test_ok(a, [[]])
*a = [1]; test_ok(a, [[1]])
*a = [nil]; test_ok(a, [[nil]])
*a = [[]]; test_ok(a, [[[]]])
*a = [1,2]; test_ok(a, [1,2])
*a = [*[]]; test_ok(a, [[]])
*a = [*[1]]; test_ok(a, [[1]])
*a = [*[1,2]]; test_ok(a, [1,2])
*a = *nil; test_ok(a, [nil])
*a = *1; test_ok(a, [1])
*a = *[]; test_ok(a, [])
*a = *[1]; test_ok(a, [1])
*a = *[nil]; test_ok(a, [nil])
*a = *[[]]; test_ok(a, [[]])
*a = *[1,2]; test_ok(a, [1,2])
*a = *[*[]]; test_ok(a, [])
*a = *[*[1]]; test_ok(a, [1])
*a = *[*[1,2]]; test_ok(a, [1,2])
a,b,*c = nil; test_ok([a,b,c], [nil,nil,[]])
a,b,*c = 1; test_ok([a,b,c], [1,nil,[]])
a,b,*c = []; test_ok([a,b,c], [nil,nil,[]])
a,b,*c = [1]; test_ok([a,b,c], [1,nil,[]])
a,b,*c = [nil]; test_ok([a,b,c], [nil,nil,[]])
a,b,*c = [[]]; test_ok([a,b,c], [[],nil,[]])
a,b,*c = [1,2]; test_ok([a,b,c], [1,2,[]])
a,b,*c = [*[]]; test_ok([a,b,c], [nil,nil,[]])
a,b,*c = [*[1]]; test_ok([a,b,c], [1,nil,[]])
a,b,*c = [*[1,2]]; test_ok([a,b,c], [1,2,[]])
a,b,*c = *nil; test_ok([a,b,c], [nil,nil,[]])
a,b,*c = *1; test_ok([a,b,c], [1,nil,[]])
a,b,*c = *[]; test_ok([a,b,c], [nil,nil,[]])
a,b,*c = *[1]; test_ok([a,b,c], [1,nil,[]])
a,b,*c = *[nil]; test_ok([a,b,c], [nil,nil,[]])
a,b,*c = *[[]]; test_ok([a,b,c], [nil,nil,[]])
a,b,*c = *[1,2]; test_ok([a,b,c], [1,2,[]])
a,b,*c = *[*[]]; test_ok([a,b,c], [nil,nil,[]])
a,b,*c = *[*[1]]; test_ok([a,b,c], [1,nil,[]])
a,b,*c = *[*[1,2]]; test_ok([a,b,c], [1,2,[]])
def f; yield; end; f {|a| test_ok(a, nil)}
def f; yield nil; end; f {|a| test_ok(a, nil)}
def f; yield 1; end; f {|a| test_ok(a, 1)}
def f; yield []; end; f {|a| test_ok(a, [])}
def f; yield [1]; end; f {|a| test_ok(a, [1])}
def f; yield [nil]; end; f {|a| test_ok(a, [nil])}
def f; yield [[]]; end; f {|a| test_ok(a, [[]])}
def f; yield [*[]]; end; f {|a| test_ok(a, [])}
def f; yield [*[1]]; end; f {|a| test_ok(a, [1])}
def f; yield [*[1,2]]; end; f {|a| test_ok(a, [1,2])}
def f; yield *nil; end; f {|a| test_ok(a, nil)}
def f; yield *1; end; f {|a| test_ok(a, 1)}
def f; yield *[]; end; f {|a| test_ok(a, nil)}
def f; yield *[1]; end; f {|a| test_ok(a, 1)}
def f; yield *[nil]; end; f {|a| test_ok(a, nil)}
def f; yield *[[]]; end; f {|a| test_ok(a, [])}
def f; yield *[*[]]; end; f {|a| test_ok(a, nil)}
def f; yield *[*[1]]; end; f {|a| test_ok(a, 1)}
def f; yield *[*[1,2]]; end; f {|a| test_ok(a, [1,2])}
def f; yield; end; f {|*a| test_ok(a, [])}
def f; yield nil; end; f {|*a| test_ok(a, [nil])}
def f; yield 1; end; f {|*a| test_ok(a, [1])}
def f; yield []; end; f {|*a| test_ok(a, [[]])}
def f; yield [1]; end; f {|*a| test_ok(a, [[1]])}
def f; yield [nil]; end; f {|*a| test_ok(a, [[nil]])}
def f; yield [[]]; end; f {|*a| test_ok(a, [[[]]])}
def f; yield [1,2]; end; f {|*a| test_ok(a, [1,2])}
def f; yield [*[]]; end; f {|*a| test_ok(a, [[]])}
def f; yield [*[1]]; end; f {|*a| test_ok(a, [[1]])}
def f; yield [*[1,2]]; end; f {|*a| test_ok(a, [1,2])}
def f; yield *nil; end; f {|*a| test_ok(a, [nil])}
def f; yield *1; end; f {|*a| test_ok(a, [1])}
def f; yield *[]; end; f {|*a| test_ok(a, [])}
def f; yield *[1]; end; f {|*a| test_ok(a, [1])}
def f; yield *[nil]; end; f {|*a| test_ok(a, [nil])}
def f; yield *[[]]; end; f {|*a| test_ok(a, [[]])}
def f; yield *[1,2]; end; f {|*a| test_ok(a, [1,2])}
def f; yield *[*[]]; end; f {|*a| test_ok(a, [])}
def f; yield *[*[1]]; end; f {|*a| test_ok(a, [1])}
def f; yield *[*[1,2]]; end; f {|*a| test_ok(a, [1,2])}
def f; yield; end; f {|a,b,*c| test_ok([a,b,c], [nil,nil,[]])}
def f; yield nil; end; f {|a,b,*c| test_ok([a,b,c], [nil,nil,[]])}
def f; yield 1; end; f {|a,b,*c| test_ok([a,b,c], [1,nil,[]])}
def f; yield []; end; f {|a,b,*c| test_ok([a,b,c], [nil,nil,[]])}
def f; yield [1]; end; f {|a,b,*c| test_ok([a,b,c], [1,nil,[]])}
def f; yield [nil]; end; f {|a,b,*c| test_ok([a,b,c], [nil,nil,[]])}
def f; yield [[]]; end; f {|a,b,*c| test_ok([a,b,c], [[],nil,[]])}
def f; yield [1,2]; end; f {|a,b,*c| test_ok([a,b,c], [1,2,[]])}
def f; yield [*[]]; end; f {|a,b,*c| test_ok([a,b,c], [nil,nil,[]])}
def f; yield [*[1]]; end; f {|a,b,*c| test_ok([a,b,c], [1,nil,[]])}
def f; yield [*[1,2]]; end; f {|a,b,*c| test_ok([a,b,c], [1,2,[]])}
def f; yield *nil; end; f {|a,b,*c| test_ok([a,b,c], [nil,nil,[]])}
def f; yield *1; end; f {|a,b,*c| test_ok([a,b,c], [1,nil,[]])}
def f; yield *[]; end; f {|a,b,*c| test_ok([a,b,c], [nil,nil,[]])}
def f; yield *[1]; end; f {|a,b,*c| test_ok([a,b,c], [1,nil,[]])}
def f; yield *[nil]; end; f {|a,b,*c| test_ok([a,b,c], [nil,nil,[]])}
def f; yield *[[]]; end; f {|a,b,*c| test_ok([a,b,c], [nil,nil,[]])}
def f; yield *[1,2]; end; f {|a,b,*c| test_ok([a,b,c], [1,2,[]])}
def f; yield *[*[]]; end; f {|a,b,*c| test_ok([a,b,c], [nil,nil,[]])}
def f; yield *[*[1]]; end; f {|a,b,*c| test_ok([a,b,c], [1,nil,[]])}
def f; yield *[*[1,2]]; end; f {|a,b,*c| test_ok([a,b,c], [1,2,[]])}
class IterTest
def initialize(e); @body = e; end
def each0(&block); @body.each(&block); end
def each1(&block); @body.each {|*x| block.call(*x)} end
def each2(&block); @body.each {|*x| block.call(x) } end
def each3(&block); @body.each {|x| block.call(*x) } end
def each4(&block); @body.each {|x| block.call(x) } end
def each5; @body.each {|*x| yield(*x)} end
def each6; @body.each {|*x| yield(x) } end
def each7; @body.each {|x| yield(*x) } end
def each8; @body.each {|x| yield(x) } end
def f(a)
test_ok(a, [1])
end
end
IterTest.new(nil).method(:f).to_proc.call([1])
IterTest.new([0]).each0 {|x| test_ok(x, 0)}
IterTest.new([1]).each1 {|x| test_ok(x, 1)}
IterTest.new([2]).each2 {|x| test_ok(x, [2])}
IterTest.new([3]).each3 {|x| test_ok(x, 3)}
IterTest.new([4]).each4 {|x| test_ok(x, 4)}
IterTest.new([5]).each5 {|x| test_ok(x, 5)}
IterTest.new([6]).each6 {|x| test_ok(x, [6])}
IterTest.new([7]).each7 {|x| test_ok(x, 7)}
IterTest.new([8]).each8 {|x| test_ok(x, 8)}
IterTest.new([[0]]).each0 {|x| test_ok(x, [0])}
IterTest.new([[1]]).each1 {|x| test_ok(x, [1])}
IterTest.new([[2]]).each2 {|x| test_ok(x, [[2]])}
IterTest.new([[3]]).each3 {|x| test_ok(x, 3)}
IterTest.new([[4]]).each4 {|x| test_ok(x, [4])}
IterTest.new([[5]]).each5 {|x| test_ok(x, [5])}
IterTest.new([[6]]).each6 {|x| test_ok(x, [[6]])}
IterTest.new([[7]]).each7 {|x| test_ok(x, 7)}
IterTest.new([[8]]).each8 {|x| test_ok(x, [8])}
IterTest.new([[0,0]]).each0 {|x| test_ok(x, [0,0])}
IterTest.new([[8,8]]).each8 {|x| test_ok(x, [8,8])}
def foo(s)
test_ok(s, "a")
end
def config_string
yield "a"
end
config_string(&method(:foo).to_proc)