(ruby-listから転載) lambda中のProc中のreturnの挙動について

$B9bHx$H?=$7$^$9!#(B
[ruby-list:47997]$B$K$F!"(Blambda$BCf$N(Bproc$B$N(Breturn$B$,[email protected]$H$$$&;]$NEj9F$r$5$;[email protected]$$$?$N$G$9$,!"([email protected]$H$$$&$3$H$G!"$3$A$i$G2~$a$FEj9F$5$;$FD:$-$^$9!#(B

$B>e5-$N%3!<%I$r<B9T$9$k$H!"(Bruby1.8.7$B$G$O(BLocalJumpError$B$,5/$3$j!"([email protected]>o=*N;$7$^$9!#(B

end
b[][]
end
a

$B$r<B9T$9$k$H!"(Bruby1.8.7$B!"([email protected]>o=*N;$7$^$9!#(B

$B$3$A$i$O!"(BProc.new$BCf$N(Breturn$B$,<[email protected]@.85$,4{$K=*N;$7$F$$$k$N$G!"(BLocalJumpError$B$,5/$3$k$N$,@5$7$$5sF0$N$h$&$K;W$$$^$9!#(B

$B$^$?!">e5-Ej9F$KBP$7!"?.2,$5$s$,>$7$/D4::$7$F2<$5$j!"0J2<$NDL$j$K$^$H$a$FD:$-$^$7$?!#(B

$BFC$K:$$i$J$$$+$H$O;W$$$^$9!Y(B $B$H$$$&H/[email protected](B)

$B$H$$$&46$8$J$N$+$J!<$H;W$$$^$7$?!#(B

$B8=>u!"(Bruby$B$N<BAuJ}?K$O>e5-$NDL$j$K$J$C$F$$$k(B
($BK\7o$N(Breturn$B$N5sF0$O%P%0$G$O$J$$(B/$B%P%[email protected]$,BP=hJ]N1Cf$G$"$k(B)
$B$HM}2r$7$FNI$$$G$7$g$&$+!)(B

$B$^$D$b$H(B $B$f$-$R$m$G$9(B

In message “Re: [ruby-dev:43423] (ruby-list$B$+$iE>:(B)
lambda$BCf$N(BProc$BCf$N(Breturn$B$N5sF0$K$D$$$F(B”
on Sat, 23 Apr 2011 19:31:28 +0900, m_takao
[email protected] writes:

|$B8=>u!"(Bruby$B$N<BAuJ}?K$O>e5-$NDL$j$K$J$C$F$$$k(B
($BK\7o$N(Breturn$B$N5sF0$O%P%0$G$O$J$$(B/$B%P%[email protected]$,BP=hJ]N1Cf$G$"$k(B)
$B$HM}2r$7$FNI$$$G$7$g$&$+!)(B

$B3N$+$KD>46E*$G$J$$ItJ,$,$"$j$^$9$7!"(B1.8$B$H(B1.9$B$GF0$-$,0[$J$C$F(B
$B$$$k$H$$$&[email protected]$O;v<B$J$N$G!"$A$g$C$HD4$Y$F$_$^$9!#$J$s$H$J$/%P(B
$B%0$N=-$$$,$7$^$9!#(B

$B?.2,$G$9!#(B
[ruby-dev:35165] $B$d(B [ruby-dev:35167]
$B$"$?$j$r;[email protected]$r9M$($?$N$G(B
$B$^$H$a$F$$-$^$9!#(B $BJdB-@bL@E$J07$$$G!#(B

$B9M$($k$Y$-LdBj$O(B 2 [email protected]$"$k$H;W$$$^$9!#(B

== 1. lambda $B$NCf$N(B proc $B$+$i$N(B return $B$N5sF0(B (Ruby 1.9
$B$H(B Ruby 1.8 $B$GF0:n$,0[$J$k(B) ==

$B$^$:(B 1 $B$D$a$O!"(Blambda $B$NCf$N(B proc $B$+$i$N(B return
$B$N5sF0$,(B Ruby 1.9 $B$H(B Ruby 1.8 $B$G0[$J$k(B
$B$H$$$&$3$H$G$9!#(B $B8=:$N(B Ruby 1.8, 1.9 $B$G$NF0:n$r$$k$H(B

$B!&(BRuby 1.9 $B$G$O!"(Bproc $BD>2<$N(B return
$B<0$,I>2A$5$l$k$H!"$=$N(B proc $B$r4^$s$G$$$k:G$bFbB&$N(B
$B!!(Blambda $B$b$7$/$O%a%=%C%I$,=*N;$9$k(B
$B!&(BRuby 1.8 $B$G$O(B proc $B$r4^$s$G$$$k%a%=%C%I$,=*N;$9$k(B (lambda
$B$OL5;k$5$l$k(B)

$B$H$J$C$F$$$^$9!#(B [ruby-dev:35175]
$B$G$N$^$D$b$H$5$s$NH/[email protected]$r$$k$H!"8=:$N(B Ruby 1.9 $B$K$$1$k(B
$BF0:n$O;EMM$I$
[email protected]$H;W$$$^$9$,!"!V$G$O(B Ruby 1.8
$B$K$*$1$kF0:n$O$3$l$G$$$$$N$+$I$&$+!W!"(B
$B$H$$$&$N$,(B 1 $B$D9M$([email protected]$+$H;W$$$^$9!#(B

($B$A$J$_$K(B [ruby-dev:35175] $B$N%5%s%W%k%3!<%I$G$O(B proc
$B%a%=%C%I$,;H$o$l$F$$$^$9$,!"(BRuby 1.8 $B$G$N(B
proc $B%a%=%C%I$O(B proc $B$G$O$J$/(B lambda
[email protected]@.$9$k$N$G!"(B[ruby-dev:35175] $B$N%5%s%W%k%3!<%I$r(B
$B$=$N$^$^(B ruby 1.8 $B$H(B 1.9
$B$NN>J}$G<B9T$9$k$HEvA3$J$,$i0[$J$kF0:n7k2L$K$J$k$H;W$$$^$9!#(B)

$B%5%s%W%k%3!<%I(B

def test1
b1 = lambda{
b2 = lambda{
Proc.new{
# $B$3$N(B return $B<0$O(B:
# Ruby 1.8 $B$G$O%a%=%C%I(B test1 $B$rH4$1$k(B <-
$B$3$l$O;EMM$I$*$j!)(B
# Ruby 1.9 $B$G$OFbB&$N(B lambda $B$rH4$1$k(B
return :test1_proc
}.call
}
$stdout << “test1-1 : #{b2.call()}\n”
:test1_lambda # not reached on ruby 1.8, but reached on ruby 1.9
}
$stdout << “test1-2 : #{b1.call()}\n”
:test1 # not reached on ruby 1.8, but reached on ruby 1.9
end
$stdout << “test1-3 : #{test1()}\n”

$B>e5-%3!<%I$N<B9T7k2L$O(B Ruby 1.8, 1.9
$B$G$=$l$>$l0J2<$N$h$&$K$J$j$^$9!#(B

ruby 1.8.7 (2010-08-16 patchlevel 302) [x86_64-linux]
test1-3 : test1_proc

ruby 1.9.2p180 (2011-02-18 revision 30909) [x86_64-linux]
test1-1 : test1_proc
test1-2 : test1_lambda
test1-3 : test1

== 2. lambda [email protected]@.$5$l$?(B proc $B$NCf$N(B return $B$,!"(Blambda
$B<B9T=*N;8e$KI>2A$5$l$?>l9g$NF0:n(B ==

$B9bHx$5$s$,[email protected]$N(B 2 $B$DL$O!"$^$5$K(B [ruby-dev:35167]
$B$G$NOCBj$HF1$8$G$9!#(B
[email protected]$5$s$,(B $B!X<BAuE*$JET9g$G$$$&$H!$!V(Blambda $B$+$i8+$F(B
orphan $B$r%A%’%C%/$9$k$NBgJQ$=$&[email protected]$J$!!W(B
$B$H;W$C$F!$BP1~=PMh$F$$$^$;$s!%[email protected]!$(Bm() $B$+$iH4$1$k$U$D!<$N(B
return $B$K$J$C$F$$$^$9!%$A$g$C$H!$(B
$B%l%"%1!<%92a$.$k$+$J$"!$$H$b;W$&$N$G!$BP1~$7$J$/$F$b!$FC$K:$$i$J$$$+$H$O;W$$$^$9!%!Y(B
[ruby-dev:35173] $B$H(B
$B6D$C$F7k6I$=$N$^$^[email protected]$H;W$$$^$9$,!"(B

$B%"(B. $B8=:_$NF0:n$,;EMM$G$"$C$F!":#8e$b$3$N$^$^$G$"$k(B
$B%$(B. $BBP1~$G$-$k$N$G$"$l$P>-Mh$O(B LocalJumpError
$B$rH/@8$5$;$k$h$&$K$9$k$+$b$7$l$J$$$N$G!"(B
$B!!$3$N$h$&$J%3!<%I$O=q$/$Y$-$G$O$J$$(B

$B$N$I$A$i$G$"[email protected]$K<($7$FD:$1$l$P(B Ruby
$B%f!<%6!<$H$7$F$O$"$j$,$?$$$G$9!#(B
(LocalJumpError
$B$rH/@8$5$;$k$N$,D>46E*$JF0:n$J$N$G!"8=>u$G$O%(%i!<$,H/@8$7$J$$$H$7$F$b(B
$B$=$N$h$&$J%3!<%I$O=q$/$Y$-$G$O$J$$$H;W$$$^$9$,!#!#(B)

$B0J>e!"[email protected]$$$D$DD9$/$J$C$F$7$^$$$9$_$^$;$s!#!#(B

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs