# Re: class local instance variable

ã“ã‚“ã°ã‚“ã¯ã€‚æœ´ èŠå° ã¨ç”³ã—ã¾ã™ã€‚

æ¬¡ã®ã‚ˆã†ãªcodeã‚’å®Ÿè¡Œã—ã¦ã¿ã¾ã—ãŸã€‚
ã™ã‚‹ã¨ã€

mithrandir@losrorien ~/ruby/ruby-svn/trunk \$ cat test2.rb
class Merong
def m
@y = “Yes!”
end
end

m = Merong.new
m.m
m.instance_eval { @x = “Yes!” }
puts m.instance_variables.inspect

mithrandir@losrorien ~/ruby/ruby-svn/trunk \$ ./miniruby test2.rb
[:@x/#<Class:#Merong:0xb7ed95c4>, :@y/Merong]

ã“ã†ãªã‚Šã¾ã—ãŸã€‚

ä½•ã‹instance_evalã®ä¸­ã§ã¯ã¡ã‚‡ã£ã¨ivar2ãŒé•ã†ã‚ˆã†ãªæ°—ãŒã—ã¾ã™ãŒã‚ã‚ŒãŒé–“é•ã£ãŸã¨ã™ã‚Œã°ã€@x/Merong
ã¨ãªã‚‹ã‚ˆã†ã«ç›´ã—ãŸã‚‰Singletonã‚‚å…ƒã®ã‚³ãƒ¼ãƒ‰ã§å‹•ãã¨æ€ã„ã¾ã™ã€‚
ã‹ã‚“ã¡ãŒã„ã ã£ãŸã‚‰ã™ã¿ã¾ã›ã‚“ã€‚

æ—¥æœ¬èªžãŒä¸‹æ‰‹ã§ã™ã€ã™ã¿ã¾ã›ã‚“ã€‚å¤§ç›®ã«ã¿ã¦ãã ã•ã„

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

In message “Re: [ruby-dev:30362] Re: class local instance variable”
on Sat, 17 Feb 2007 01:31:28 +0900, “Park Ji-In” [email protected]
writes:

|mithrandir@losrorien ~/ruby/ruby-svn/trunk \$ cat test2.rb
|class Merong
| def m
| @y = “Yes!”
| end
|end
|
|m = Merong.new
|m.m
|m.instance_eval { @x = “Yes!” }
|puts m.instance_variables.inspect
|
|mithrandir@losrorien ~/ruby/ruby-svn/trunk \$ ./miniruby test2.rb
|[:@x/#<Class:#Merong:0xb7ed95c4>, :@y/Merong]
|
|e\$B\$3\$&\$J\$j\$^\$7\$?!#e(B

instance_evale\$B\$G\$O%/%i%9%m!<%+%k\$NBP>]\$H\$J\$k%/%i%9\$O!"\$=\$N%*e(B
e\$B%V%8%'%/%H\$NFC0[%/%i%9\$G\$9!#\$G\$9\$+\$i!“e(BMeronge\$B%/%i%9\$N%/%i%9%m!<e(B
e\$B%+%kJQ?t\$O%”%/%;%9\$G\$-\$^\$;\$s!#e(B

e\$B\$G\$b!“\$3\$3\$G\$Oe(BMeronge\$B%/%i%9\$N%m!<%+%k%\$%s%9%?%s%9JQ?t\$K%”%/%;e(B
e\$B%9\$G\$-\$k\$H\$\$\$&\$N\$,0lHLE*\$J4|BT\$J\$s\$G\$7\$g\$&\$+!#%/%i%9JQ?t\$N\$He(B
e\$B\$-\$K\$b\$\$\$m\$\$\$m9M\$(\$^\$7\$?e(B[1]e\$B\$,!"F1\$8\$h\$&\$J\$3\$H\$r%/%i%9%m!<%+e(B
e\$B%kJQ?t\$KBP\$7\$F\$b9T\$&\$Y\$-\$J\$s\$G\$7\$g\$&\$+!#e(B

ç¨²ç”°ã§ã™ã€‚

ã‚’ä½¿ã†ã¨ãã¯å¤§æŠµã€æ—¢ã«å­˜åœ¨ã™ã‚‹ã‚ªãƒ–ã‚¸ã‚§ã‚¯ãƒˆã‚’ç›´æŽ¥æ“ä½œ/å‚ç…§ã™ã‚‹ã¤ã‚‚ã‚Šã§æ›¸ã„ã¦ã„ã‚‹ã®ã§ã€

2. ãã®ã‚ªãƒ–ã‚¸ã‚§ã‚¯ãƒˆã®ã‚¤ãƒ³ã‚¹ã‚¿ãƒ³ã‚¹å¤‰æ•°ã«ã¯ã™ã¹ã¦ã‚¢ã‚¯ã‚»ã‚¹ã§ãã‚‹ã¨æ€ã£ã¦ã—ã¾ã†ã€

instance_eval ã§ãªãã¦ã‚ªãƒ–ã‚¸ã‚§ã‚¯ãƒˆã«ç‰¹ç•°ãƒ¡ã‚½ãƒƒãƒ‰ã‚’è¿½åŠ ã™ã‚‹å ´åˆã§ã‚‚ã€åŒã˜ã‚ˆã†ã«æ„Ÿã˜ã¾ã™ã€‚

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

e\$B\$A\$g\$C\$H9M\$(\$?\$s\$G\$9\$,!"\$^\$@G<F@\$G\$-\$k7kO@\$,=P\$F\$\$\$^\$;\$s!#e(B

In message “Re: [ruby-dev:30382] Re: class local instance variable”
on Sun, 18 Feb 2007 12:23:18 +0900, “Makoto Inada”
[email protected] writes:

|> e\$B\$G\$b!“\$3\$3\$G\$Oe(BMeronge\$B%/%i%9\$N%m!<%+%k%\$%s%9%?%s%9JQ?t\$K%”%/%;%9\$G\$-\$k\$H\$\$\$&\$N\$,0lHLE*\$J4|BT\$J\$s\$G\$7\$g\$&\$+!#e(B
|
|e\$B;d\$O\$=\$&4|BT\$7\$^\$9!#%W%m%0%i%`\$r=q\$\$\$F\$\$\$Fe(B instance_eval |e\$B\$r;H\$&\$H\$-\$OBgDq!"4{\$KB8:_\$9\$k%*%V%8%'%/%H\$rD>@\A`:ne(B/e\$B;2>H\$9\$k\$D\$b\$j\$G=q\$\$\$F\$\$\$k\$N\$G!"e(B
|
|1. e\$BFC0[%/%i%9\$NB8:_\$r!J\$?\$H\$(CN\$C\$F\$\$\$F\$b!K0U<1\$7\$F\$\$\$J\$\$!“e(B
|2. e\$B\$=\$N%*%V%8%'%/%H\$N%\$%s%9%?%s%9JQ?t\$K\$O\$9\$Y\$F%”%/%;%9\$G\$-\$k\$H;W\$C\$F\$7\$^\$&!"e(B
|
|e\$B\$H\$\$\$&\$U\$&\$K9M\$(\$F\$\$\$^\$9!#e(B

e\$B\$^\$:!“e(B1e\$B\$K\$D\$\$\$F\$G\$9\$,!“e(Binstance_evale\$B\$G\$N%?!<%2%C%H\$H\$J\$k%/%ie(B
e\$B%9\$,FC0[%/%i%9\$G\$J\$\$\$H\$^\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B0@h\$r!”\$=\$N%
%V%8%'%/%H\$NFC0[%/%i%9\$K\$9\$k\$N\$+!”\$=\$N%*e(B
e\$B%V%8%'%/%H\$N%/%i%9\$K\$9\$k\$N\$+\$OG:\$^\$7\$\$\$H\$3\$m\$G\$9!#e(B

instance_evale\$B\$Ne(B(e\$B%/%i%9%m!<%+%k%\$%s%9%?%s%9JQ?t\$Ne(B)e\$B%?!<%2%C%He(B
e\$B\$r!“\$=\$N%*%V%8%'%/%H\$N%/%i%9\$K\$9\$k\$N\$Oe(B(e\$B\$d\$d<BAu\$,LLE]\$J\$b\$Ne(B
e\$B\$Ne(B)e\$BIT2DG=\$G\$O\$J\$\$\$N\$G\$9\$,!”\$=\$l\$K\$I\$l\$@\$10UL#\$,\$"\$k\$N\$+\$h\$/e(B
e\$B\$o\$+\$i\$J\$\$\$H8@\$&\$N\$,@5D>\$J\$H\$3\$m\$G\$9!#e(B

e\$B0J2<\$N3F2U=j\$G\$N%/%i%9%m!<%+%k%\$%s%9%?%s%9JQ?t\$O\$I\$N%/%i%9\$Ke(B
e\$B=jB0\$9\$Y\$-\$@\$H;W\$\$\$^\$9\$+!)e(B

class Foo
def foo
@_foo # (1)
end
def self.foo_c1
@_foo # (2)
end
end

class <<Foo
def foo_c2
@_foo # (3)
end
end

def Foo.foo_c3
@_foo # (4)
end

foo = Foo.new

def foo.foo_i1
@_foo # (5)
end

class <<foo
def.foo_i2
@_foo # (6)
end
end

foo.instance_eval {
def foo_i3
@_foo # (7)
end
}

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

ã¨ã„ã†ã®ã‚‚ã€ç¨²ç”°ã•ã‚“ã®æœŸå¾…ã®å‰æã¨ãªã‚‹ã€Œãã®ã‚ªãƒ–ã‚¸ã‚§ã‚¯ãƒˆã®ã‚¤
ãƒ³ã‚¹ã‚¿ãƒ³ã‚¹å¤‰æ•°ã«ã¯ã™ã¹ã¦ã‚¢ã‚¯ã‚»ã‚¹ã§ãã‚‹ã€ã¨ã„ã†ä»®å®šã¯ã€ãã‚Œã
ã®ã‚‚ã®ãŒã‚¢ã‚¯ã‚»ã‚¹ã§ãã‚‹ç¯„å›²ã‚’é™å®šã™ã‚‹(ã®ã§ã€åç§°é‡è¤‡ãŒã‚ã£ã¦
ã™ã‚‹ã‹ã‚‰ã§ã™ã€‚

class Foo
def foo
@_foo # (1) Foo
end
def self.foo_c1
@_foo # (2) Class
end
end

class <<Foo
def foo_c2
@_foo # (3) Class
end
end

def Foo.foo_c3
@_foo # (4) Class
end

foo = Foo.new

def foo.foo_i1
@_foo # (5) Foo
end

class <<foo
def.foo_i2
@_foo # (6) Foo
end
end

foo.instance_eval {
def foo_i3
@_foo # (7) Foo
end
}

åŒã˜ã“ã¨ã®ç¹°ã‚Šè¿”ã—ã«ãªã‚Šã¾ã™ãŒã€ç§ã«ã¨ã£ã¦ç‰¹ç•°ãƒ¡ã‚½ãƒƒãƒ‰ã¨ã„ã†ã®ã¯ 1. æ—¢å­˜ã®ã‚ªãƒ–ã‚¸ã‚§ã‚¯ãƒˆã«å¾Œã‹ã‚‰ãƒ¡ã‚½ãƒƒãƒ‰ã‚’ä»˜ã‘è¶³ã›ã‚‹ã€2.