From: matz@... (Yukihiro Matsumoto) Date: 2003-01-01T10:06:07+09:00 Subject: [ruby-dev:19264] Re: ruby-1.8.0 / yield(nil)とyield()のちがい まつもと ゆきひろです In message "[ruby-dev:19219] Re: ruby-1.8.0 / yield(nil)とyield()のちがい" on 02/12/28, Yukihiro Matsumoto writes: |おとといから悩んでるんですが、異様に難しいです。が、そのうち |になんとかします。 なんとかなったみたいです。以下のtest suiteが動作します。もう じきコミットします。 # 次は定数問題[ruby-talk:59972]だ。 def test_ok(a, b) if a != b print "failed at ", caller(0)[1], " != \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 [*[]]; 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 *[*[]]; 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 [*[]]; 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 *[*[]]; 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])}