From: pharrington Date: 2009-11-29T04:30:05+09:00 Subject: Re: Local variables can't be accessed from outside, right? On Nov 28, 1:15 pm, Michael Winterstein wrote: > Benoit Daloze wrote: > > > I didn't find other way to get the result of class A ... end like a if > > or > > case result. > > Very weared a class' instance variable(1) > > Does it have any interest? > > No, it's just an example for learning about metaprogramming.  Which is > why it seems strange; the local variables can only be 'accessed' because > they happen to be return values. > > I did learn something I didn't know, however : > > class A >     puts "Local variables: #{local_variables}" >     puts "Instance variables: #{instance_variables}" >     a = 5 >     @a = 6 >     puts "Local variables: #{local_variables}" >     puts "Instance variables: #{instance_variables}" > end > > Local variables: a > Instance variables: > Local variables: a > Instance variables: @a > > So local variables exist in some ethereal void after the code is scanned > but before they can be used; not so with instance variables.  That makes > some sense.  But this also lets you do this eval weirdness. > From what I understand, this is because local variables are created when code is *parsed*, not actually run (as method resolution would be even more painful if the interpreter had to first search the object tree for a particular instance method every time a new local variable was defined)