From: Marnen Laibow-Koser Date: 2009-11-28T15:30:31+09:00 Subject: Re: Local variables can't be accessed from outside, right? Michael Winterstein wrote: > Hi, I've been trying to figure out metaprogramming and I've mostly got > it figured out, but in doing so I ran across this example: > > class A > def initialize > @a = 11 > @@a = 22 > a = 33 > end > @a = 1 > @@a = 2 > a = 3 > end > > > This was given as a posed question (can you access all the variables) > and a particular order of the output was given (specifically, 1 2 3 11 > 22 33). Getting at the instance variables, and even the class variables > doesn't seem that hard to me. But it seems to me there's no way to get > at the local variable a's. The way it was asked makes me think it might > be possible, but I'm reasonably sure it isn't. > > However, I'm fairly new to Ruby and just wrapping my mind around the > metaprogramming ideas and realize I could easily be missing something. > > I think a trick like puts A.new would be a lame solution, Well, it would be the only solution to a lame question. :) > finding it somehow in memory after the class definition end statement > (though that might be interesting to know if Ruby can do that). Sort of. Remember, a class definition is executable code. Does that give you an idea? It should. Best, -- Marnen Laibow-Koser http://www.marnen.org marnen@marnen.org -- Posted via http://www.ruby-forum.com/.