Binding の仕様変更? or バグ?


#1

e$B1J0fe(B@e$BCNG=!%6e9)Bg$G$9!%e(B

e$BF|K\e(B Ruby e$B%+%s%U%!%l%s%9$N@J$Ge(B
e$B$3$N$H$3$m$N%3%%C%H$N1F6A$Ge(B Ruby/Tk
e$B4X78$,%(%i!<$r=P$9$h$&$Ke(B
e$B$J$C$F$$$k$H$$$&OC$rJ9$$$?$N$G;n$7$F$
$?$H$3$m!$e(B
e$B3N$+$KLdBj$,H/@8$7$F$$$^$7$?!%e(B

e$B<B$N$H$3$m!$;d$K$H$C$F$O!V:$$k!W$I$3$m$+e(B
e$B5^=j$X$NL?Cf$H$b8@$($k$[$I$NBg%@%a!<%8$H$J$kLdBj$G$9!%e(B(;_:wink:

e$BNc$($P<!$N$h$&$J%9%/%j%W%H$,$"$C$?$H$7$^$9!%e(B
----< hoge.rb >---------------------------------------
def hoge(*args)
[self, args]
end

p hoge(1, self)
Object.new.instance_eval{ p hoge(2, self)}

e$B$3$l$rC1=c$Ke(B ruby hoge.rb e$B$H$9$l$P!$Nc$($Pe(B

[main, [1, main]]
[#Object:0x401d5cb4, [2, #Object:0x401d5cb4]]

e$B$H=PNO$5$l$^$9!%e(B
e$B$b$A$m$se(B ruby -e load hoge.rb e$B$H$7$F$bF1MM$G$9!%e(B

e$B$G!$%0%m!<%P%k$JL>A06u4V$r1x@w$9$k$3$H$J$/<B9T$7$?$$$H9M$($Fe(B
----< hoge2.rb >--------------------------------------
def null_binding
Module.new.module_eval{binding}
end
eval(File.read(hoge.rb, null_binding))

e$B$H$7$Fe(B ruby hoge2.rb e$B$J$I$H$7$?>l9g!$e(B
e$B>/$7A0$^$G$Ne(B ruby-1.8 (e$B>/$J$/$H$be(B ruby-1.8.4
e$B$N%j%j!<%9HGe(B) e$B$G$Oe(B

[#Module:0x401d5db2, [1, #Module:0x401d5db2]]
[#Object:0x401d5cb4, [2, #Object:0x401d5cb4]]

e$B$J$I$H$$$&$h$&$K!$$&$^$/e(B(?)e$BF0$$$F$/$l$F$$$^$7$?!%e(B

e$B$7$+$7!$8=:_$Ne(B ruby 1.8.4 (2006-06-11) [i686-linux] e$B$G$Oe(B

hoge2.rb:6:in null_binding': undefined methodhoge for
#Module:0x401d5d90 (NoMethodError)
from hoge2.rb:2:in null_binding' from hoge2.rb:2:innull_binding
from hoge2.rb:4

e$B$H$J$j$^$9!%e(B

e$B$3$&$7$?$3$H$Oe(B ruby -e load hoge.rb, true e$B$N$h$&$J>l9g$Oe(B
e$B$G$-$k$Y$-$J$N$G$O$J$$$+$H$b;W$&$N$G$9$,!$$3$l$be(B

[main, [1, main]]
./hoge.rb:6: undefined method `hoge for #Object:0x401d5a20
(NoMethodError)
from ./hoge.rb:6
from -e:1

e$B$H$J$C$F$7$^$C$F%@%a$G$9!%e(B

e$B!V%b%8%e!<%k$N%3%s%F%-%9%H$GI>2A$9$k$s$@$+$i!$$=$&$J$C$FEv$?$jA0!%e(B
e$B0JA0$K$G$-$F$$$?$3$H$NJ}$,%P%0!We(B

e$B$H$*$C$7$c$k$+$b$7$l$^$;$s$,!$e(B
load e$B$G$b%@%a!$e(Bbinding e$B$G$b%@%a$H$$$&$3$H$K$J$k$H!$e(B
e$B4X?t7?$N%a%=%C%IDj5A$r9T$C$F$$$k%9%/%j%W%H$r$=$N$^$^$Ke(B
e$B%0%m!<%P%k$JL>A06u4V$r1x@w$5$;$:$K<B9T$9$k<jCJ$,$"$j$^$;$s!%e(B
e$B$=$N$?$a!$Hs>o$K:$$C$?>u67$K$J$C$F$$$^$9!%e(B

e$B$G$-$l$P0JA0$NF0:n$KLa$7$FM_$7$$$N$G$9$,!$e(B
e$B$b$7!$;EMM>e!$0JA0$N>uBV$K$OLa$9$3$H$O@dBP$K$G$-$J$$$N$G$"$l$P!$e(B
e$B$<$R$H$b;j5^$KBe49<jCJ$rDs6!$$$?$@$1$^$9$h$&6/$/$*4j$$CW$7$^$9!%e(B

e$B<B$OJL$N<jCJ$,B8:_$7$F$$$k$N$G$7$?$i$4$a$s$J$5$$!%e(B

e$B$=$N>l9g$O$<$R$H$b65$($r@A$$$?$/;W$$$^$9!%e(B


#2

e$B1J0fe(B@e$BCNG=!%6e9)Bg$G$9!%e(B

From: Hidetoshi NAGAI removed_email_address@domain.invalid
Subject: [ruby-dev:28718] binding e$B$N;EMMJQ99!)e(B or e$B%P%0!)e(B
Date: Mon, 12 Jun 2006 15:19:32 +0900
Message-ID: removed_email_address@domain.invalid

e$B$<$R$H$b;j5^$KBe49<jCJ$rDs6!$$$?$@$1$^$9$h$&6/$/$*4j$$CW$7$^$9!%e(B

e$B$A$g$C$H>G$C$F$F!$6/$$I=8=$G=q$$$F$7$^$C$F$^$7$?!%$9$_$^$;$s!%e(B
e$B$G$9$,!$$D$$$=$&=q$$$F$7$^$C$?$[$I$K:$$C$F$$$k$N$OK\Ev$G$9!%e(B
e$B$I$&$+$h$m$7$/$*4j$$$7$^$9!%e(B


#3

?@?????

???????????????????????(;_:wink:

??????????????????????
???????????????????
( ???) ????????? ????????
CVS ruby_1_8 ????????

Module#toplevel_eval module_eval ??????????????
??????????????????
? ???????????????

  • ???????????????????????????????????

  • ::CONST ?????????????????????????
    ??????????????????????????????????
    ? ?????? (?????) ???????????????

?????????
????????????????????????????????????
?????????????????? load ????????
??::CONST = val ?????????????????????????
???????????

???????????????????????? ??
??????????????
? ???????????????requrie (autoload ??) ?
???? ??require ???????????????

?????????????????????

???????module_eval ?????????

module M
HOGE = 111
def foo(*args)
p [self, args]
end
end

class X
end

########################################

begin
p -----(1)-----
M.module_eval{foo(1,2,3)}
rescue
p $!
end
#=> #<NoMethodError: undefined method `foo for M:Module>

begin
p -----(2)-----
M.toplevel_eval{foo(1,2,3)}
rescue
p $!
end
#=> [M, [1, 2, 3]]

begin
p -----(3)-----
M.module_eval{X.new.instance_eval{foo(1,2,3)}}
rescue
p $!
end
#=> #<NoMethodError: undefined method `foo for #<X:0x401d1308>>

begin
p -----(4)-----
M.toplevel_eval{X.new.instance_eval{foo(1,2,3)}}
rescue
p $!
end
#=> [#<X:0x401d1178>, [1, 2, 3]]

begin
p -----(5)-----
M.module_eval{Object.new.instance_eval{foo(1,2,3)}}
rescue
p $!
end
#=> #<NoMethodError: undefined method `foo for #Object:0x401d1074>

begin
p -----(6)-----
M.toplevel_eval{Object.new.instance_eval{foo(1,2,3)}}
rescue
p $!
end
#=> [#Object:0x401d0ed0, [1, 2, 3]]

begin
p -----(9)-----
M.module_eval{Object.new.instance_eval{p HOGE}}
rescue
p $!
end
#=> #<NameError: uninitialized constant HOGE>

begin
p -----(10)-----
M.toplevel_eval{Object.new.instance_eval{p HOGE}}
rescue
p $!
end
#=> 111

begin
p -----(11)-----
M.module_eval{X.new.instance_eval{p HOGE}}
rescue
p $!
end
#=> #<NameError: uninitialized constant HOGE>

begin
p -----(12)-----
M.toplevel_eval{X.new.instance_eval{p HOGE}}
rescue
p $!
end
#=> 111

begin
p -----(13)-----
M.module_eval{p ::HOGE}
rescue
p $!
end
#=> #<NameError: uninitialized constant HOGE>

begin
p -----(14)-----
M.toplevel_eval{p ::HOGE}
rescue
p $!
end
#=> 111

begin
p -----(15)-----
M.module_eval{p ::M}
rescue
p $!
end
#=> M

begin
p -----(16)-----
M.toplevel_eval{p ::M}
rescue
p $!
end
#=> M

begin
p -----(17)-----
M.module_eval{Object.new.instance_eval{p ::HOGE}}
rescue
p $!
end
#=> #<NameError: uninitialized constant HOGE>

begin
p -----(18)-----
M.toplevel_eval{Object.new.instance_eval{p ::HOGE}}
rescue
p $!
end
#=> 111

begin
p -----(19)-----
M.module_eval{X.new.instance_eval{p ::HOGE}}
rescue
p $!
end
#=> #<NameError: uninitialized constant HOGE>

begin
p -----(20)-----
M.toplevel_eval{X.new.instance_eval{p ::HOGE}}
rescue
p $!
end
#=> 111

begin
p -----(21)-----
M.module_eval{
def hoge(*args)
p [self, args]
end
hoge(:a, :b, :c)
Object.new.instance_eval{hoge(:d, :e, :f)}
}
rescue
p $!
end
#=> #<NoMethodError: undefined method `hoge for M:Module>

begin
p -----(22)-----
M.toplevel_eval{
def fuga(*args)
p [self, args]
end
fuga(:a, :b, :c)
Object.new.instance_eval{fuga(:d, :e, :f)}
}
rescue
p $!
end
#=> [M, [:a, :b, :c]]
#=> [#Object:0x401d04bc, [:d, :e, :f]]

p --------------------------

p m = Module.new #=> #Module:0x401d0480
m.toplevel_eval{
FOO = :foo
::BAR = :bar

def baz(*args)
[self, args]
end

p baz(1, FOO, ::FOO, ::BAR)
#=> [#Module:0x401d0480, [1, :foo, :foo, :bar]]
p Object.new.instance_eval{baz(2, FOO, ::FOO, ::BAR)}
#=> [#Object:0x401d02dc, [2, :foo, :foo, :bar]]
}
p m.constants #=> [BAR, FOO]


#4

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

In message Re: [ruby-dev:28735] Module#toplevel_eval (Re: binding
e$B$N;EMMJQ99!)e(B or e$B%P%0!)e(B)
on Thu, 15 Jun 2006 08:35:03 +0900, Hidetoshi NAGAI
removed_email_address@domain.invalid writes:

|e$B1J0fe(B@e$BCNG=!%6e9)Bg$G$9!%e(B
|
|# e$BJQ$J$3$H$P$+$j$d$C$F$k$s$GA4$/Aj<j$K$5$l$F$J$$$G$9$M!%!%!%e(B(;_:wink:

[ruby-dev:28718]e$B$K$D$$$F$J$i!8+$F$^$9!#$?$@!$^$@$I$3$,0-$5e(B
e$B$7$F$$$k$N$+J,$+$C$F$^$;$s!#$A$g$C$HBT$C$F$/$@$5$$$M!#e(B


#5

e$B1J0fe(B@e$BCNG=!%6e9)Bg$G$9!%e(B

From: Yukihiro M. removed_email_address@domain.invalid
Subject: [ruby-dev:28736] Re: Module#toplevel_eval (Re: binding
e$B$N;EMMJQ99!)e(B or e$B%P%0!)e(B)
Date: Thu, 15 Jun 2006 16:02:58 +0900
Message-ID: removed_email_address@domain.invalid

[ruby-dev:28718]e$B$K$D$$$F$J$i!8+$F$^$9!#$?$@!$^$@$I$3$,0-$5e(B
e$B$7$F$$$k$N$+J,$+$C$F$^$;$s!#$A$g$C$HBT$C$F$/$@$5$$$M!#e(B

e$B$"$j$,$H$&$4$6$$$^$9!%$h$m$7$/$*4j$$CW$7$^$9!%e(B

e$B$A$J$_$K@Z1)5M$^$C$F:n$C$?e(B Module#toplevel_eval e$B$G$9$,!$e(B
e$B$=$N3d$K$O7k9=;H$($=$&$G!$!V$A$g$C$H$$$$$+$b!W$H;W$C$F$$$^$9!%e(B

e$BNc$($P$G$9$,!$%;%-%e%j%F%#>e$NM}M3$G!$FI$_9~$s$@%3!<%I$+$i$Ne(B
e$BDj?te(B ENV e$B$X$N%"%/%;%9$K@)8B$r$+$1$?$$>l9g$,$"$C$?$H$7$^$9!%e(B
e$B$"$i$f$k%1!<%9$r9M$($F$$$k$o$1$G$O$J$$$G$9$,!$<!$N$h$&$K$9$k$He(B
Module#toplevel_eval
e$B$NCf$+$i@)Ls$r2sHr$9$k$N$O$+$J$jFq$7$=$&$G$9!%e(B

========================================================
class Wrapped_ENV
@@allow_keys = [USER, DISPLAY]

def initialize
@env = ENV
end

def inspect
Wrapped_ENV
end

def class
nil
end

def _check_key(key)
@@allow_keys.index(key)
end
private :_check_key

def
key = key.to_s
(_check_key(key))? @env[key]: nil
end

def []=(key,val)
key = key.to_s
val = val.to_s
@env[key] = val if _check_key(key)
end
end

module XX
ENV = Wrapped_ENV.new

ObjectSpace = nil
Wrapped_ENV = nil

def remove_const(*args)
raise SecurityError, deny to remove constants
end
end

#########################

puts *** access `ENV in main
p [ENV[USER], ENV[USER]]
p [ENV[DISPLAY], ENV[DISPLAY]]
p [ENV[HOME], ENV[HOME]]

XX.toplevel_eval{
puts \n*** access `ENV in toplevel_eval
p ENV
p [ENV[USER], ENV[USER]]
p [ENV[DISPLAY], ENV[DISPLAY]]
p [ENV[HOME], ENV[HOME]]
}

XX.toplevel_eval{
puts \n*** try to `remove_const :ENV
begin
remove_const :ENV
rescue
p $!
end
}

XX.toplevel_eval{
puts \n*** try to override `remove_const to call super()
def remove_const(*args)
super(*args)
end
begin
remove_const :ENV
rescue
p $!
end

def self.remove_const(*args)
super(*args)
end
begin
self.remove_const :ENV
rescue
p $!
end
}

XX.toplevel_eval{
puts \n*** try to `undef_method :remove_const
begin
undef_method :remove_const
rescue
p $!
end
begin
class << self
undef_method :remove_const
end
rescue
p $!
end
begin
remove_const :ENV
rescue
p $!
end
}

XX.toplevel_eval{
puts \n*** try to access ObjectSpace
p ObjectSpace
p ::ObjectSpace

puts \n*** try to access Wrapped_ENV
p Wrapped_ENV
p ::Wrapped_ENV
}

puts \n
XX.toplevel_eval{p ENV}

e$B<B:]$K<B9T$9$k$H!$$3$&$J$j$^$9!%e(B

========================================================
*** access `ENV in main
[ENV[USER], nagai]
[ENV[DISPLAY], :0.0]
[ENV[HOME], /home/nagai]

*** access `ENV in toplevel_eval
Wrapped_ENV
[ENV[USER], nagai]
[ENV[DISPLAY], :0.0]
[ENV[HOME], nil]

*** try to `remove_const :ENV
#<SecurityError: deny to remove constants>

*** try to override remove_const' to call super() #<NoMethodError: super: no superclass methodremove_const>
#<NoMethodError: super: no superclass method `remove_const>

*** try to undef_method :remove_const' #<NameError: undefined methodremove_const for class Class'> #<NoMethodError: undefined methodremove_const for XX:Class>

*** try to access ObjectSpace
nil
nil

*** try to access Wrapped_ENV
nil
nil

Wrapped_ENV

e$B$$$+$,$J$b$N$G$7$g$&!)e(B
e$B>/$7$O%;%-%e%j%F%#6/2=$KLrN)$?$J$$$G$7$g$&$+!)e(B(^_^;


#6

e$B1J0fe(B@e$BCNG=!%6e9)Bg$G$9!%e(B

From: Yukihiro M. removed_email_address@domain.invalid
Subject: [ruby-dev:28780] Re: binding e$B$N;EMMJQ99!)e(B or e$B%P%0!)e(B
Date: Wed, 21 Jun 2006 17:45:13 +0900
Message-ID: removed_email_address@domain.invalid

e$B$H$$$&$3$H$G!"$h$&$d$C$H;~4V$r$H$C$FD4$Y$F$_$^$7$?!#0JA0$3$le(B
e$B$,F0$$$F$$$?$H$$$&$N$,$I$&$K$bG<F@$,9T$+$J$$$N$G!8E$$%P!<%8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ge(B1.8e$B$G$bBLL$G$O$J$$e(B
    e$B$H;W$&$N$G$9$,!#e(Bevale$B$,A}$($k$H7y$J4i$r$9$k?M$,L$KIb$+$Ve(B
    e$B$h$&$G$9!#e(B

toplevel_eval e$B$NCf?H$Oe(B module_eval e$B$HF1$8$G$9!%e(B
e$B0c$&$N$O%/%i%93,AX$rC)$kM%@h=g0L$K$A$g$C$H$@$1:Y9)$re(B
e$B2C$($F$$$kE@$N$_e(B (e$B$@$C$?$H;W$&e(B) e$B$J$N$G$9$,!$e(B
e$B$=$l$G$be(B YARV e$B$K$OBg$-$/1F6A$9$k$G$7$g$&$+!)e(B >
e$B$5$5$@$5$se(B

e$B$b$&$A$g$C$H9M$($5$;$F$/$@$5$$!#$40U8+Jg=8Cf!#e(B

e$B85!9$N5sF0$,%P%0$G$"$C$?$H$9$k$H!$e(B
e$B$<$Re(B toplevel_eval
e$B$^$?$O$=$N=$@5J*$rF3F~$7$F$$$?$@$-$?$$$H;W$$$^$9!%e(B
e$B$G$J$1$l$P!$$G$-$?J}$,K>$^$7$$e(B (e$B$H;W$($ke(B) e$B$3$H$,e(B
e$B$G$-$=$&$G$G$-$J$$$H$$$&$3$H$K$J$k$N$G$O$J$$$G$7$g$&$+!%e(B

load(file, true) e$B$K$D$$$F$b!$$=$l$,4|BT$5$;$k$3$H$r9M$($k$J$i!$e(B
e$BL5L>%b%8%e!<%k$G$Ne(B toplevel_eval
e$BAjEv$H$7$?J}$,NI$5$=$&$K46$8$^$9!%e(B
e$B$?$@$7!$e(Bload(file, true) e$B$NJQ99$OHs8_49$r4^$_$^$9$N$Ge(B
e$B$3$l$r:#2s$N%j%j!<%9$GJQ99$9$k$3$H$OL5M}$J$N$G$7$g$&$M!%e(B


#7

e$B1J0fe(B@e$BCNG=!%6e9)Bg$G$9!%e(B

From: Yukihiro M. removed_email_address@domain.invalid
Subject: [ruby-dev:28780] Re: binding e$B$N;EMMJQ99!)e(B or e$B%P%0!)e(B
Date: Wed, 21 Jun 2006 17:45:13 +0900
Message-ID: removed_email_address@domain.invalid

  • toplevel_evale$B$NF3F~!#$^$"!"DI2C$J$N$Ge(B1.8e$B$G$bBLL$G$O$J$$e(B
    e$B$H;W$&$N$G$9$,!#e(Bevale$B$,A}$($k$H7y$J4i$r$9$k?M$,L$KIb$+$Ve(B
    e$B$h$&$G$9!#e(B

[ruby-dev:28735] e$B$de(B [ruby-dev:28737] e$B$NF0:n$r<($7$F!$e(B
e$B!V$3$&$7$?;EMM$Ne(B Module#toplevel_eval e$B$NF3F~$r$I$&;W$&$+!)!We(B
e$B$He(B ruby-list e$B$de(B ruby-talk
e$B$KN.$7$F0U8+$r5a$a$?J}$,$$$$$G$7$g$&$+!)e(B


#8

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

In message Re: [ruby-dev:28718] binding e$B$N;EMMJQ99!)e(B or
e$B%P%0!)e(B
on Mon, 12 Jun 2006 15:19:32 +0900, Hidetoshi NAGAI
removed_email_address@domain.invalid writes:

|e$B<B$N$H$3$m!$;d$K$H$C$F$O!V:$$k!W$I$3$m$+e(B
|e$B5^=j$X$NL?Cf$H$b8@$($k$[$I$NBg%@%a!<%8$H$J$kLdBj$G$9!%e(B(;_:wink:
|
|e$BNc$($P<!$N$h$&$J%9%/%j%W%H$,$"$C$?$H$7$^$9!%e(B
|----< hoge.rb >---------------------------------------
|def hoge(*args)
| [self, args]
|end
|
|p hoge(1, self)

Object.new.instance_eval{ p hoge(2, self)}
e$B$3$l$rC1=c$Ke(B ruby hoge.rb e$B$H$9$l$P!$Nc$($Pe(B
------------------------------------------------------
[main, [1, main]]
[#Object:0x401d5cb4, [2, #Object:0x401d5cb4]]
------------------------------------------------------
e$B$H=PNO$5$l$^$9!%e(B
e$B$b$A$m$se(B ruby -e load hoge.rb e$B$H$7$F$bF1MM$G$9!%e(B
e$B$G!$%0%m!<%P%k$JL>A06u4V$r1x@w$9$k$3$H$J$/<B9T$7$?$$$H9M$($Fe(B
----< hoge2.rb >--------------------------------------
def null_binding
Module.new.module_eval{binding}
end
eval(File.read(hoge.rb, null_binding))
------------------------------------------------------
e$B$H$7$Fe(B ruby hoge2.rb e$B$J$I$H$7$?>l9g!$e(B
e$B>/$7A0$^$G$Ne(B ruby-1.8 (e$B>/$J$/$H$be(B ruby-1.8.4 e$B$N%j%j!<%9HGe(B) e$B$G$Oe(B
------------------------------------------------------
[#Module:0x401d5db2, [1, #Module:0x401d5db2]]
[#Object:0x401d5cb4, [2, #Object:0x401d5cb4]]
------------------------------------------------------
e$B$J$I$H$$$&$h$&$K!$$&$^$/e(B(?)e$BF0$$$F$/$l$F$$$^$7$?!%e(B

e$B$H$$$&$3$H$G!"$h$&$d$C$H;~4V$r$H$C$FD4$Y$F$_$^$7$?!#0JA0$3$le(B
e$B$,F0$$$F$$$?$H$$$&$N$,$I$&$K$bG<F@$,9T$+$J$$$N$G!8E$$%P!<%8%ge(B
e$B%s$r2~$a$F%3%s%Q%$%k$7$FD4$Y$kI,MW$,$$j$^$7$?!#e(B

e$B7kO@$+$i$$$&$H!e(B1.8.3e$B$^$G$N%P!<%8%g%s$G$O$3$N%9%/%j%W%H$O3Ne(B
e$B$+$KF0:n$7$^$9$,!$=$l$O%0%m!<%P%k$JL>A06u4Ve(B(Object)e$B$Ke(Bhogee$B%ae(B
e$B%=%C%I$rDj5A$7$F$$$?$+$i$G$9!#$D$^$j!"85!9$N!V%0%m!<%P%k$JL>e(B
e$BA06u4V$r1x@w$9$k$3$H$J$/<B9T$7$?$$!W$H$$$&MW5a$rK~$?$7$F$$$^e(B
e$B$;$s!#e(B

e$B$G$O!"$I$&$9$k$+!"$G$9$,!"e(B

  • e$B!V85$N5sF0$KLa$9!W$N$OIT2DG=$G$O$J$$$N$G$9$,!"!V%P%0$rI|e(B
    e$B3h$5$;$k!W$H$$$&$3$H$J$N$G!"$"$^$j>h$j5$$G$O$"$j$^$;$s!#e(B
    e$B$^$?!85!9$NMW5a$rK~$?$7$F$$$J$$0J>e$$^$j0UL#$,$J$$$h$&e(B
    e$B$J5$$,$7$^$9!#e(B

  • e$B$J$K$b$7$J$$!"$@$He(Btke$B$G$O:$$k$s$G$9$h$M!#e(B

  • toplevel_evale$B$NF3F~!#$^$"!"DI2C$J$N$Ge(B1.8e$B$G$bBLL$G$O$J$$e(B
    e$B$H;W$&$N$G$9$,!#e(Bevale$B$,A}$($k$H7y$J4i$r$9$k?M$,L$KIb$+$Ve(B
    e$B$h$&$G$9!#e(B

e$B$b$&$A$g$C$H9M$($5$;$F$/$@$5$$!#$40U8+Jg=8Cf!#e(B


#9

e$B1J0fe(B@e$BCNG=!%6e9)Bg$G$9!%e(B

From: Yukihiro M. removed_email_address@domain.invalid
Subject: [ruby-dev:28784] Re: binding e$B$N;EMMJQ99!)e(B or e$B%P%0!)e(B
Date: Wed, 21 Jun 2006 21:09:25 +0900
Message-ID: removed_email_address@domain.invalid

|[ruby-dev:28735] e$B$de(B [ruby-dev:28737] e$B$NF0:n$r<($7$F!$e(B
|e$B!V$3$&$7$?;EMM$Ne(B Module#toplevel_eval e$B$NF3F~$r$I$&;W$&$+!)!We(B
|e$B$He(B ruby-list e$B$de(B ruby-talk e$B$KN.$7$F0U8+$r5a$a$?J}$,$$$$$G$7$g$&$+!)e(B

e$B$H$a$^$;$s$1$I!"@dBP$KI,MW$+$H$$$o$l$k$H$=$&$G$b$J$$$h$&$K;We(B
e$B$$$^$9e(B(e$BFC$Ke(Bruby-talk)e$B!#8@8l;EMM$K$D$$$F$&$k$5$$$N$O05E]E*$Ke(B
e$BF|K?M$,B?$$$G$9$+$i!#e(B

e$B$G$OB>=j$KFC$K2?$+$rN.$9$h$&$J$3$H$O$;$:!$e(B
e$B$^$D$b$H$5$s$NH=CG$r$*BT$A$7$^$9!%e(B

e$BI,$:$7$be(B Module#toplevel_eval e$B$G$"$kI,MW$O$J$$$N$G$9$,!$e(B
e$B2?$i$+$N<BAu<jCJ$O$<$R$H$bM_$7$$e(B (e$B8D?ME*$K$O$G$9$,e(B)
e$B$H$3$m$G$9$N$G!$e(B
e$B$I$&$+$h$m$7$/$*4j$$CW$7$^$9!%e(B


#10

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

In message Re: [ruby-dev:28737] Re: Module#toplevel_eval
on Thu, 15 Jun 2006 21:14:49 +0900, Hidetoshi NAGAI
removed_email_address@domain.invalid writes:

|e$B$A$J$_$K@Z1)5M$^$C$F:n$C$?e(B Module#toplevel_eval e$B$G$9$,!$e(B
|e$B$=$N3d$K$O7k9=;H$($=$&$G!$!V$A$g$C$H$$$$$+$b!W$H;W$C$F$$$^$9!%e(B

e$B$h$&$d$C$H%Q%C%A$rFI$_$^$7$?!#$J$s$+%a%=%C%I%-%c%C%7%e$r9MN8e(B
e$B$7$F$J$$$h$&$J5$$,$9$k$s$G!F1$8L>A0$N%a%=%C%I$,$$k$H4|BT$7e(B
e$B$F$J$$F0$-$r$9$k$h$&$J5$$,$7$^$9!#$+$H$$$C$F!"$I$&D>$;$P$h$$e(B
e$B$N$+$O$o$+$i$J$$$N$G$9$,!#e(B

e$B$I$&$K$+$7$F$"$2$?$$$H$3$m$G$O$"$k$N$G$9$,!#e(B


#11

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

In message Re: [ruby-dev:28783] Re: binding e$B$N;EMMJQ99!)e(B or
e$B%P%0!)e(B
on Wed, 21 Jun 2006 19:12:00 +0900, Hidetoshi NAGAI
removed_email_address@domain.invalid writes:

|[ruby-dev:28735] e$B$de(B [ruby-dev:28737] e$B$NF0:n$r<($7$F!$e(B
|e$B!V$3$&$7$?;EMM$Ne(B Module#toplevel_eval e$B$NF3F~$r$I$&;W$&$+!)!We(B
|e$B$He(B ruby-list e$B$de(B ruby-talk e$B$KN.$7$F0U8+$r5a$a$?J}$,$$$$$G$7$g$&$+!)e(B

e$B$H$a$^$;$s$1$I!"@dBP$KI,MW$+$H$$$o$l$k$H$=$&$G$b$J$$$h$&$K;We(B
e$B$$$^$9e(B(e$BFC$Ke(Bruby-talk)e$B!#8@8l;EMM$K$D$$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$b4^$a$?e(B)e$BDj?t$NJ]8ne(B
  • e$B4X?tE*%a%=%C%I$re(B(e$B$I$3$G$be(B)e$B4X?tE*$K8F$S$?$$e(B

e$B$N#2E@$G$7$g$&$+!#A0<T$O$J$s$H$+$J$j$=$&$J5$$,$7$^$9$,!"8e<Te(B
e$B$Oe(B(e$B$$$^$@;EMM$5$(Dj$+$G$J$$e(B)classboxe$B$H$+e(Bselector
namespacee$B$He(B
e$B$[$\F1MM$N5!G=$G$9$M$(!#e(B

e$B$"$k$$$O!"@PDM$5$s$Ne(Bclass-in-statee$B$_$?$$$J$N$r<BAu$9$k$+e(Be$B!#e(B
e$B$b$&$A$g$C$H9M$($5$;$F$/$@$5$$!#e(B

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

#13

e$B1J0fe(B@e$BCNG=!%6e9)Bg$G$9!%e(B

From: Yukihiro M. removed_email_address@domain.invalid
Subject: [ruby-dev:28793] Re: Module#toplevel_eval
Date: Thu, 22 Jun 2006 12:42:45 +0900
Message-ID: removed_email_address@domain.invalid

e$B$h$&$d$C$H%Q%C%A$rFI$_$^$7$?!#$J$s$+%a%=%C%I%-%c%C%7%e$r9MN8e(B
e$B$7$F$J$$$h$&$J5$$,$9$k$s$G!F1$8L>A0$N%a%=%C%I$,$$k$H4|BT$7e(B
e$B$F$J$$F0$-$r$9$k$h$&$J5$$,$7$^$9!#$+$H$$$C$F!"$I$&D>$;$P$h$$e(B
e$B$N$+$O$o$+$i$J$$$N$G$9$,!#e(B

e$B%@%a$G$7$?$+!%!%!%e(B
e$BBg92$F$G:n$C$?$b$N$O=jA$=$NDxEY$NBeJ*$@$C$?$h$&$G$9$M!%e(B(;_:wink:

e$B%a%=%C%I$dDj?t$r8!:w$7$F$$$kItJ,$K3d$j9~$a$P$$$$$H;W$C$F$^$7$?$,!$e(B

e$B$=$3$^$GC1=c$K$O:Q$^$;$i$l$J$+$C$?$h$&$G$9$M!%e(B

e$B%-%c%C%7%e$K$OA4$/L$,8~$$$F$$$^$;$s$G$7$?!%e(B

e$B$I$&$K$+$7$F$"$2$?$$$H$3$m$G$O$"$k$N$G$9$,!#e(B

e$B$48|0U$r$"$j$,$H$&$4$6$$$^$9!%e(B

e$B$7$+$7$J$,$i!$$[$H$s$IH,J}:I$,$j$N>u67$G$9$M!%e(B(T_T)

e$B$=$l$H$b%a%=%C%I%-%c%C%7%e$N7o$,$I$&$K$+$G$-$5$($9$l$Pe(B
e$B$^$@2DG=@-$O$"$k$N$G$7$g$&$+!)e(B

e$BNc$($P!$%-%c%C%7%e$K!V%H%C%W%l%Y%k$G8+$D$1$?$b$N$+$I$&$+!W$Ne(B
e$B%U%i%0$bJ];}$9$ke(B
(e$B%a%=%C%I$,8+$D$+$i$J$+$C$?$H$-$b%U%i%0$rN)$F$F!$e(B
e$B$=$N;~E@$N%H%C%W%l%Y%k$^$?$Oe(B rb_cObject e$B$re(B origin
e$B$K%;%C%H$9$ke(B)
e$B$3$H$K$7$F!$e(B

if (klass e$B$He(B mid e$B$,0lCW$9$k$b$N$,B8:_e(B)

if (e$B!V%H%C%W%l%Y%k$G8+$D$1$?!W%U%i%0$,N)$C$F$$$ke(B)

  if (e$B!V8=:_$N%H%C%W%l%Y%k!W$,@_Dj$5$l$F$$$ke(B)
     if (origin e$B$Oe(B rb_cObject e$B$G$"$ke(B)
        e$B%-%c%C%7%e$NFbMF$OL58ze(B ( 

e$B8=:$N%H%C%W%l%Y%k$N$$r:F8!:we(B )
elsif (origin e$B$O8=:_$N%H%C%W%l%Y%k$G$"$ke(B)
e$B%-%c%C%7%e$NFbMF$OM-8ze(B ( method == 0
e$B$b!V$J$7!W$H$7$FM-8ze(B )
else
e$B%-%c%C%7%e$NFbMF$OL58ze(B ( e$BIaDL$K%U%k8!:we(B )
end

  else
     if (origin e$B$Oe(B rb_cObject e$B$G$"$ke(B)
        e$B%-%c%C%7%e$NFbMF$OM-8ze(B ( method == 0 

e$B$b!V$J$7!W$H$7$FM-8ze(B )
else
e$B%-%c%C%7%e$NFbMF$OL58ze(B ( rb_cObject e$B$N$_$r:F8!:we(B
)
end
end

else
e$B%-%c%C%7%e$NFbMF$OM-8ze(B
end

else
e$BIaDL$K%U%k8!:we(B
end

e$B$H$$$&$h$&$JH=Dj$G$O%@%a$G$7$g$&$+!)e(B

e$B%H%C%W%l%Y%k@_Dj$N$"$je(B/e$B$J$7$r9T$C$?$jMh$?$j$9$k>l9g$K$Oe(B
e$B%-%c%C%7%e%R%C%HN($,$+$J$j2<$,$k2DG=@-$,$"$j$^$9$,!$e(B
e$B%U%k8!:w$9$k$h$j$O>/$7$O%3%9%H$,Dc$/$G$-$=$&$K;W$($^$9!%e(B

e$B$-$C$H$^$?$I$3$+$,H4$1$F$$$F%@%a$J$s$G$7$g$&$M!%e(B(^_^;


#14

e$B1J0fe(B@e$BCNG=!%6e9)Bg$G$9!%e(B

From: Yukihiro M. removed_email_address@domain.invalid
Subject: [ruby-dev:28796] Re: Module#toplevel_eval
Date: Thu, 22 Jun 2006 18:17:32 +0900
Message-ID: removed_email_address@domain.invalid

e$B$&!<$s!"<B8=$5$;$?$$$3$H$Oe(B

  • (e$B%H%C%W%l%Y%k$b4^$a$?e(B)e$BDj?t$NJ]8ne(B

e$B$3$A$i$Oe(B

e$B!V%b%8%e!<%k$NCf$G$H$$$&@)Ls2<$GI>2A$7$h$&$H$7$F$$$k$N$K!$e(B
e$B$=$N%b%8%e!<%k$Ke(B CONST e$B$rDj5A$7$F$$$?$H$7$F$be(B
::CONST e$B$G%H%C%W%l%Y%k$Ne(B CONST e$B$K$"$C$5$j%"%/%;%9$,e(B
e$B$G$-$F$7$^$&$N$C$F7y$8$c$J$$!)!We(B

e$B$H$$$&46$8$G!$e(B

  • e$B4X?tE*%a%=%C%I$re(B(e$B$I$3$G$be(B)e$B4X?tE*$K8F$S$?$$e(B

e$B$3$A$i$Oe(B

e$B!V%W%m%0%i%JR$rJq$_9~$s$G<B9T$7$?$$$H$$$&$@$1$NMW5a$J$N$K!$e(B e$BJq$_9~$$b$N$,$?$^$?$^%b%8%e!<%k$G$"$k$;$$$Ge(B
e$B$=$N%W%m%0%i%`JR$NCf$G4X?tE*$K:n$i$l;H$o$l$F$$$k%a%=%C%I$,e(B
e$B8F$Y$J$/$J$C$A$c$&$N$O7y$8$c$J$$!)!We(B

e$B$H$$$&46$8$G$9!%e(B

e$B$N#2E@$G$7$g$&$+!#A0<T$O$J$s$H$+$J$j$=$&$J5$$,$7$^$9$,!"8e<Te(B
e$B$Oe(B(e$B$$$^$@;EMM$5$(Dj$+$G$J$$e(B)classboxe$B$H$+e(Bselector namespacee$B$He(B
e$B$[$\F1MM$N5!G=$G$9$M$(!#e(B

e$B$"$k$$$O!"@PDM$5$s$Ne(Bclass-in-statee$B$_$?$$$J$N$r<BAu$9$k$+e(Be$B!#e(B
e$B$b$&$A$g$C$H9M$($5$;$F$/$@$5$$!#e(B

e$B$4LBOG$r$+$1$F$^$9$,!$$h$m$7$/$*4j$$CW$7$^$9!%e(B


#15

e$B1J0fe(B@e$BCNG=!%6e9)Bg$G$9!%e(B

From: Hidetoshi NAGAI removed_email_address@domain.invalid
Subject: [ruby-dev:28797] Re: Module#toplevel_eval
Date: Thu, 22 Jun 2006 18:48:55 +0900
Message-ID: removed_email_address@domain.invalid

e$B$N#2E@$G$7$g$&$+!#A0<T$O$J$s$H$+$J$j$=$&$J5$$,$7$^$9$,!"8e<Te(B
e$B$Oe(B(e$B$$$^$@;EMM$5$(Dj$+$G$J$$e(B)classboxe$B$H$+e(Bselector namespacee$B$He(B
e$B$[$\F1MM$N5!G=$G$9$M$(!#e(B

e$B$"$k$$$O!"@PDM$5$s$Ne(Bclass-in-statee$B$_$?$$$J$N$r<BAu$9$k$+e(Be$B!#e(B
e$B$b$&$A$g$C$H9M$($5$;$F$/$@$5$$!#e(B

e$B$4LBOG$r$+$1$F$^$9$,!$$h$m$7$/$*4j$$CW$7$^$9!%e(B

e$B$J$+$J$+BP1~$,Fq$7$=$&$J>u67$G$9$N$G!$e(B
e$B!VNI$$$d$jJ}$G$O$J$$$h$J$!!W$H;W$$$D$De(B (^_^;
e$B1~5^=hCVE*$K2<5-%3!<%I$N$h$&$J@oN,$G$NBP:v$rE,MQ$7$^$7$?!%e(B

e$B$^$!$J$s$H$+:GDc8B$NHO0O$G$O8mKb2=$;$F$$$k$h$&$G$O$"$k$N$G$9$,!$e(B
e$BB>$H>WFM$7$d$9$=$&$J>lEv$?$jE*BP=h$G$9$N$G!$e(B
e$B$d$O$j??$CEv$JBP1~$,K>$^$l$^$9!%e(B
1.8.5 e$B$N%j%j!<%9D>A0$G$OBP1~$r4V$K9g$o$;$k$N$,Fq$7$/$J$k62$l$be(B
e$B$"$j$^$9$N$G!$#1F|$G$bAa$/$KBP1~$7$F$$$?$@$1$k$H4r$7$/;W$$$^$9!%e(B

e$B2f$,PV$P$+$j$G$9$_$^$;$s$,!$$h$m$7$/$*4j$$CW$7$^$9!%e(B

class Object
alias method_missing method_missing
private :method_missing

def method_missing(id, *args)
begin
has_top = (top = Thread.current[:TOPLEVEL]) &&
top.respond_to?(:pseudo_toplevel_evaluable?) &&
top.pseudo_toplevel_evaluable? &&
top.respond_to?(id)
rescue Exception => e
has_top = false
end

if has_top
  top.__send__(id, *args)
else
  __method_missing__(id, *args)
end

end
end

class Proc
def initialize(*args, &b)
super
@pseudo_toplevel = Thread.current[:TOPLEVEL]
end

alias call call
def call(*args, &b)
if top = @pseudo_toplevel
orig_top = Thread.current[:TOPLEVEL]
Thread.current[:TOPLEVEL] = top
begin
call(*args, &b)
ensure
Thread.current[:TOPLEVEL] = orig_top
end
else
call(*args, &b)
end
end
end

def proc(&b)
Proc.new(&b)
end
def lambda(&b)
Proc.new(&b)
end

module PseudoToplevel_Evaluable
def pseudo_toplevel_eval(body = Proc.new)
orig_top = Thread.current[:TOPLEVEL]
Thread.current[:TOPLEVEL] = self
begin
body.call
ensure
Thread.current[:TOPLEVEL] = orig_top
end
end

def pseudo_toplevel_evaluable?
@pseudo_toplevel_evaluable
end
def pseudo_toplevel_evaluable=(mode)
@pseudo_toplevel_evaluable = (mode)? true: false
end

def self.extended(mod)
mod.send(:extend_object, mod)
mod.instance_variable_set(@pseudo_toplevel_evaluable, true)
end
end

module M
extend PseudoToplevel_Evaluable

def foo(*args)
p [self, :foo, args]
end
end

class A
end

p M.methods.sort
p M.instance_methods.sort
puts 1: A.new.instance_eval{foo(1,2,3)}
begin
A.new.instance_eval{foo(1,2,3)}
rescue => e
p e
end

puts 2: M.pseudo_toplevel_eval{A.new.instance_eval{foo(1,2,3)}}

begin
M.pseudo_toplevel_eval{A.new.instance_eval{foo(1,2,3)}}
rescue => e
p e
end

puts 3:
M.pseudo_toplevel_eval{A.new.instance_eval{self.foo(1,2,3)}}
begin
M.pseudo_toplevel_eval{A.new.instance_eval{self.foo(1,2,3)}}
rescue => e
p e
end

puts 4: foo(1,2,3)
begin
foo(1,2,3)
rescue => e
p e
end

puts 5: M.pseudo_toplevel_eval{foo(1,2,3)}
begin
M.pseudo_toplevel_eval{foo(1,2,3)}
rescue => e
p e
end

require tk
puts 6: TkButton.new{p foo(1,2,3)}
begin
TkButton.new{p foo(1,2,3)}
rescue => e
p e
end

puts 7: M.pseudo_toplevel_eval{TkButton.new{p foo(1,2,3)}}
begin
M.pseudo_toplevel_eval{TkButton.new{p foo(1,2,3)}}
rescue => e
p e
end

begin
M.pseudo_toplevel_eval{
l = TkLabel.new.pack
cnt = 0
TkButton.new(:command=>proc{l.text = foo(cnt); cnt+=1}){p
foo(1,2,3)}.pack
}
rescue => e
p e
end

Tk.mainloop