Block_given? in defined method

[email protected]$G$9!%e(B

e$B!!e(Bblock_given? e$B$r!$e(Bdefine_method
e$B$GDj5A$5$l$?$b$N$KE,MQ$5$l$k$H$*$+e(B
e$B$7$J$3$H$K$J$k!$$H$$$&;XE&$K$D$$$F65$($F2<$5$$!%e(B

e$B!!$3$N;XE&$O!$e(Bbootstraptest/test_knwonbug.rb
e$B$K%F%9%H%1!<%9$,$"$j$^$9!%e(B

assert_equal ‘ok’, %q{
class C
define_method(:foo) {
if block_given?
:ok
else
:ng
end
}
end
C.new.foo {}
}, ‘[ruby-core:14813]’

assert_equal ‘ok’, %q{
class C
define_method(:foo) {
if block_given?
:ng
else
:ok
end
}
end
C.new.foo
}, ‘[ruby-core:14813]’

e$B!!e(Bblock_given? e$B$O!$B?J,e(B yield e$B$d$ie(B Proc.new
e$B$J$I$,=PMh$k$+$I$&$+$r3Ne(B
e$BG’[email protected]$H;W$&$N$G$9$,!$e(Bdefine_method
e$B$GDj5A$7$?%a%=%C%I$Ge(B
e$B$O$I$A$i$b=PMh$^$;$s!J8=>u$G$O!K!%$J$N$G!$e(Bblock_given?
e$B$b!$$=$NMQESe(B
e$B$K$O;H$($J$$0J>e!$%F%9%H%1!<%9$N$h$&$K$9$k$N$O$^$:$$$H;W$&$N$G$9$,!$e(B
e$B$$$+$,$G$7$g$&$+!%e(B

e$B!!$3$N>l9g!$$I$A$i$be(B false e$B$K$J$k$Y$-$J$h$&$J5$$,$7$^$9!%e(B

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

In message “Re: [ruby-dev:34747] block_given? in defined method”
on Mon, 19 May 2008 13:32:48 +0900, SASADA Koichi [email protected]
writes:

|e$B!!e(Bblock_given? e$B$r!$e(Bdefine_method e$B$GDj5A$5$l$?$b$N$KE,MQ$5$l$k$H$*$+e(B
|e$B$7$J$3$H$K$J$k!$$H$$$&;XE&$K$D$$$F65$($F2<$5$$!%e(B

|e$B!!e(Bblock_given? e$B$O!$B?J,e(B yield e$B$d$ie(B Proc.new e$B$J$I$,=PMh$k$+$I$&$+$r3Ne(B
|e$BG’[email protected]$H;W$&$N$G$9$,!$e(Bdefine_method e$B$GDj5A$7$?%a%=%C%I$Ge(B
|e$B$O$I$A$i$b=PMh$^$;$s!J8=>u$G$O!K!%$J$N$G!$e(Bblock_given? e$B$b!$$=$NMQESe(B
|e$B$K$O;H$($J$$0J>e!$%F%9%H%1!<%9$N$h$&$K$9$k$N$O$^$:$$$H;W$&$N$G$9$,!$e(B
|e$B$$$+$,$G$7$g$&$+!%e(B

e$B$(!<$H!"$G$-$J$$$s$G$7$?$C$1!#$=$l$rG’<1$7$F$$$^$;$s$G$7$?!#e(B
lambdae$B$O%V%m%C%/<u$1IU$1$^$9$h$M!#$G!“e(Bdefine_methode$B$GDj5A$5$le(B
e$B$?%a%=%C%[email protected]$&$N$Oe$N?<$$M}M3$,$”$k$s$G$7$ge(B
e$B$&$+!#e(B

|e$B!!$3$N>l9g!$$I$A$i$be(B false e$B$K$J$k$Y$-$J$h$&$J5$$,$7$^$9!%e(B

e$BBh0l$NA*Br;h$Oe(Bdefine_methode$B$GDj5A$5$l$?%a%=%C%I$+$i$be(Byielde$B$Ge(B
e$B$-$F$[$7$$$s$G$9$,!"$J$s$i$+$N;v>p$G$=$l$,2DG=$G$J$$$J$i$P!"e(B
e$B$=$N>l9g$ON>J}e(Bfalsee$B$G$bNI$$$H;W$$$^$9!#e(B

                            e$B$^$D$b$He(B e$B$f$-$R$me(B /:|)

[email protected]$G$9!%e(B

Yukihiro M. wrote:

|e$B!!e(Bblock_given? e$B$O!$B?J,e(B yield e$B$d$ie(B Proc.new e$B$J$I$,=PMh$k$+$I$&$+$r3Ne(B
|e$BG’[email protected]$H;W$&$N$G$9$,!$e(Bdefine_method e$B$GDj5A$7$?%a%=%C%I$Ge(B
|e$B$O$I$A$i$b=PMh$^$;$s!J8=>u$G$O!K!%$J$N$G!$e(Bblock_given? e$B$b!$$=$NMQESe(B
|e$B$K$O;H$($J$$0J>e!$%F%9%H%1!<%9$N$h$&$K$9$k$N$O$^$:$$$H;W$&$N$G$9$,!$e(B
|e$B$$$+$,$G$7$g$&$+!%e(B

e$B$(!<$H!"$G$-$J$$$s$G$7$?$C$1!#$=$l$rG’<1$7$F$$$^$;$s$G$7$?!#e(B
lambdae$B$O%V%m%C%/<u$1IU$1$^$9$h$M!#$G!“e(Bdefine_methode$B$GDj5A$5$le(B
e$B$?%a%=%C%[email protected]$&$N$Oe$N?<$$M}M3$,$”$k$s$G$7$ge(B
e$B$&$+!#e(B

e$B!!e(Blambda e$B$He(B define_method
e$B$GDj5A$7$?%a%=%C%I!J0J2<!$e(Bdme$B!K$OF1DxEY$K%Ve(B
e$B%m%C%/$r<u$1IU$1$^$9!%e(B

l = lambda{|&b|
p b
yield #=> #<LocalJumpError: no block given (yield)>
}

class C
define_method(:dm){|&b|
p b
yield #=> #<LocalJumpError: no block given (yield)>
}
end

begin
l.call{}
rescue LocalJumpError => e
p e
end

begin
C.new.dm{}
rescue LocalJumpError => e
p e
end

e$B!!e(BYARV e$B$G$Oe(B yield
e$B$NDj5A$r!$?7$7$$JQ?t%9%3!<%W$r:[email protected]$9$k%?%$%_%s%0$Ge(B
e$BEO$5$l$?%V%m%C%/>pJs$rMxMQ$7$F9T$&?7$7$$%9%3!<%W$N:[email protected]!&<B9T$H$7$F$$e(B
e$B$^$9!%e(Bdefine_method
e$B$GDj5A$5$l$?%V%m%C%/$O!$8F$S=P$7$F$b?7$7$$%9%3!<e(B
e$B%W$O:n$i$J$$$?$a!J%V%m%C%/$N30$N%9%3!<%W$r0z$-7Q$0$?$a!K!$e(Bdm
e$B$KEO$7e(B
e$B$?%V%m%C%/$OBP>]$K$G$-$^$;$s!%[email protected]$7!$%V%m%C%/$re(B block
e$B0z?t!Je(B&be$B!K$Ge(B
e$B<u$1$k$3$H$,=PMh$k$N$G!$$=$l$r;H$*$&$H$$$&J}[email protected]$C$?$H;W$$$^$9!%e(B

e$B!!$3$N%k!<%k$O!$7k6I$[$+$N%V%m%C%/[email protected]$($ke(B
e$B$+$H;W$$$^$9!%$D$^$j!$0J2<$N$h$&$JNc$G$O!$e(Bsetup
e$B$KEO$7$?%V%m%C%/$,8Fe(B
e$B$P$l$^$9$,!$$3$l$O;zLL>e!$B>$N%V%m%C%/$He(B
lambdae$B!$e(Bdefine_method e$BEy$Ge(B
e$BF1$85sF0$G$9!%e(B

class C
end

def setup
$l = lambda{|&b|
p b
yield #=> block in main
}

C.module_eval{
define_method(:dm){|&b|
p b
yield #=> block in main
}
}
end

setup{
puts “block in main”
}

$l.call{}
C.new.dm{}

e$B!!e(Bdefine_method e$B$rFCJL07$$$9$k$N$J$i!$:G=i$+$ie(B define_method
e$B$r0U<1e(B
e$B$7$?:n$j$K$7$J$$$HB.EYE*$KFq$7$$$+$J!$$H;W$$$^$9!%B.EY$r5$$K$7$J$$$Ne(B
e$B$J$i!$$b$7$+$7$?$i$=$s$J$KFq$7$/$J$$$+$b$7$l$^$;$s!%e(B

e$B$?$7$+!$$3$N$"$?$j$O5nG/!$L5M}$G$9!$4*J[$7$F2<$5$$!$e(B

[email protected]$C$F$$$?$h$&$J5$$,$9$k!%e(B

|e$B!!$3$N>l9g!$$I$A$i$be(B false e$B$K$J$k$Y$-$J$h$&$J5$$,$7$^$9!%e(B

e$BBh0l$NA*Br;h$Oe(Bdefine_methode$B$GDj5A$5$l$?%a%=%C%I$+$i$be(Byielde$B$Ge(B
e$B$-$F$[$7$$$s$G$9$,!"$J$s$i$+$N;v>p$G$=$l$,2DG=$G$J$$$J$i$P!"e(B
e$B$=$N>l9g$ON>J}e(Bfalsee$B$G$bNI$$$H;W$$$^$9!#e(B

e$B!!>e5-$NM}M3$GG<[email protected]$7$F$b$i$($k$N$J$ie(B false
e$B$K$"$o$;$k$h$&$K$7$^$9!%e(B
e$B$=$&$G$J$/!$$d$O$je(B define_method
e$B$OFCJL07$$$7$m!$$H$$$&[email protected]$C$?e(B
e$B$i!$$b$&>/$79M$($5$;$F2<$5$$!%e(B

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

In message “Re: [ruby-dev:34753] Re: block_given? in defined method”
on Mon, 19 May 2008 19:03:10 +0900, SASADA Koichi [email protected]
writes:

|e$B!!e(Blambda e$B$He(B define_method e$B$GDj5A$7$?%a%=%C%I!J0J2<!$e(Bdme$B!K$OF1DxEY$K%Ve(B
|e$B%m%C%/$r<u$1IU$1$^$9!%e(B

e$B$^$H$a$k$H%V%m%C%/0z?t$H$7$F$OEO$k$1$I!“e(Byielde$B$O8F$Y$J$$$H$$e(B
e$B$&$3$H$G$$$$$G$9$+!#$3$l$C$F!”$?$H$($P$=$N%V%m%C%/$N30B&$Ge(B
yielde$B$,[email protected]$C$?$i$=$C$A$,8F$P$l$k$H$$$&$3$H$G$$$$$G$9$+!)e(B

e$B3N$+$a$F$_$l$P$$$$$N$+!#e(B

def foo
l = lambda{|&b|
p b
p block_given?
yield
}
end

foo{p :foo}.call
foo{p :foo}.call{p :bar}

e$B$r<B9T$9$k$He(B

nil
true
:foo
#Proc:[email protected]/tmp/y.rb:9
true
:foo

e$B$G$9$+!#$J$k$[$I$M!"$"$k0UL#!“[email protected]$,$”[email protected]$($P$"$j$^$9$M!#e(B

|e$B!!$3$N%k!<%k$O!$7k6I$[$+$N%V%m%C%/[email protected]$($ke(B
|e$B$+$H;W$$$^$9!%$D$^$j!$0J2<$N$h$&$JNc$G$O!$e(Bsetup e$B$KEO$7$?%V%m%C%/$,8Fe(B
|e$B$P$l$^$9$,!$$3$l$O;zLL>e!$B>$N%V%m%C%/$He(B lambdae$B!$e(Bdefine_method e$BEy$Ge(B
|e$BF1$85sF0$G$9!%e(B

|e$B!!>e5-$NM}M3$GG<[email protected]$7$F$b$i$($k$N$J$ie(B false e$B$K$"$o$;$k$h$&$K$7$^$9!%e(B
|e$B$=$&$G$J$/!$$d$O$je(B define_method e$B$OFCJL07$$$7$m!$$H$$$&[email protected]$C$?e(B
|e$B$i!$$b$&>/$79M$($5$;$F2<$5$$!%e(B

e$BM}2r$7$^$7$?!#$3$l$G$$$$$s$8$c$J$$$G$7$g$&$+!#%F%9%H$b=q$-49e(B
e$B$($F$/[email protected]$5$$!#e(B

                            e$B$^$D$b$He(B e$B$f$-$R$me(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