Eval question

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?

···


Justin Johnson.

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.

···

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?


.^. .-------------------------------------------------------.
/V\ | Tom Gilbert, London, England | http://linuxbrit.co.uk |
/( )\ | Open Source/UNIX consultant | tom@linuxbrit.co.uk |
^^-^^ `-------------------------------------------------------’

I see. Thanks.

Is there anyway other way to create locals without explicitly setting them?

···


Justin Johnson.

“Tom Gilbert” tom@linuxbrit.co.uk wrote in message
news:20020724113537.GR6415@offended.co.uk

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 |
^^-^^ `-------------------------------------------------------’