On Monday 24 November 2003 06:40 pm, Sean E Russell wrote:
> On Monday 24 November 2003 11:51, T. Onoma wrote:
> > In such a simple case, of course. But consider:
> >
> >   a=[1,2,3]
> >   eval "def z; p a; end;  z"
>
> But... this doesn't work in Ruby anyway.  'a' isn't in scope inside the
> method.  Cut, paste, and run:
>
> 	a = [1,2,3]
> 	def z
> 		p a
> 	end
> 	z
>
> Why should method definitions in evals behave any differently?

What's the point of having eval, then? Look at your example. By your 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. Eval is for constructing code piecemeal with various variables, not just 
to do what can already be done without it. But as it stands, one must build 
literals within strings to accomplish such things. That's a lot of extra work 
and makes for choppy ugly code.

Of course, the above example dosen't really do what I want it to. For that, I 
have to get fancy: 

  p "#{a.join(',')}"

I don't like getting fancy.

-t0