On Monday 24 November 2003 14:02, T. Onoma wrote:
> What's the point of having eval, then? Look at your example. By your

My example was taken directly from your example, where you said:

> > >   a=[1,2,3]
> > >   eval "def z; p a; end;  z"

I wouldn't expect this to work; it wouldn't even work if you rewrote it:

eval "a = [1,2,3] ; def z ; p a ; end ; z"

> argument there's no point in eval at all. Would you say the same thing
> about:
>
>   a = [1,2,3]
>   eval %Q{
>     def z
>       p "#{a}"
>     end
>     z
>   }
>
> Should that be 'wrong' b/c "this doesn't work in Ruby anyway. 'a' isn't in
> scope inside the method."

No.  It isn't wrong at all.  Scope doesn't enter into it.

I think you're confusing string expansion, scoping, and late binding.  #{a} 
isn't an eval() thing; it isn't a scoping thing.  #{} doesn't have any 
special meaning to eval().  The string expansion is handled by the string 
class (or whatever), before the string ever gets to eval.

That code works like I expect it to do: turn "a" into a string using to_s() 
and replace #{a} with the value -- THEN pass the result to eval().

	eval 'p a'

and

	eval 'p #{a}'

are two different things.  In the first, 'a' is bound to the array in the eval 
environment.  In the second, there is no 'a'.

The Tick: "Spoon!"
Neo: "There is no spoon."

--- SER