From: Tom Gilbert Date: 2002-07-24T20:35:44+09:00 Subject: Re: Eval question * Justin Johnson (justinj@mobiusent.com) wrote: > I've got a question about 'eval'. In the following example (which doesn't > work by the way) where does 'val' live? > > class MyClass > def tryIt( str ) > eval str > p val > end > end > > a = MyClass.new > a.tryIt( "val = 10" ) > > I was initially thinking that 'val' would be treated as a local to 'tryIt'. > Maybe 'eval' works a little differently to how I thought it might? It's local to the scope of the string being eval'd. Try this: class MyClass def tryIt( str ) val = eval str p val end end a = MyClass.new a.tryIt( "val = 10" ) Tom. -- .^. .-------------------------------------------------------. /V\ | Tom Gilbert, London, England | http://linuxbrit.co.uk | /( )\ | Open Source/UNIX consultant | tom@linuxbrit.co.uk | ^^-^^ `-------------------------------------------------------'