来自 The Ruby programming 的 8.3.1 Querying, Setting, and Testing Variables
Note that eval evaluates its code in a temporary scope. eval can alter the value of instance variables that already exist. But any new instance variables it defines are local to the invocation of eval and cease to exist when it returns. (It is as if the evaluated code is run in the body of a block—variables local to a block do not exist outside the block.)
看这段话应该是新的实例变量在 eval 执行后会消失,但测试发现并没有 (@z变量)
[code]
irb(main):001:0> class Point
irb(main):002:1> def initialize(x,y)
irb(main):003:2> @x,@y=x,y
irb(main):004:2> end
irb(main):005:1> end
=> nil
irb(main):006:0> p=Point.new(1,2)
=> #
irb(main):007:0>
irb(main):008:0* def get_bind
irb(main):009:1> binding
irb(main):010:1> end
=> nil
irb(main):011:0> b=p.get_bind
=> #Binding:0x81915b0
irb(main):012:0>
irb(main):013:0* eval("@x",b)
=> 1
irb(main):014:0>
irb(main):015:0* eval("@x=10",b)
=> 10
irb(main):016:0> eval("@x",b)
=> 10
irb(main):017:0>
irb(main):018:0* eval("@z=2",b)
=> 2
irb(main):019:0> eval("@z",b)
=> 2
irb(main):020:0> p.instance_variables
=> [:@x, :@y, :@z]
irb(main):021:0>
irb(main):022:0*
irb(main):023:0*
[/code]