On Tue, Jul 13, 2004 at 02:32:22PM +0900, Benny wrote:
> Mauricio Fern?ndez wrote:
> 
> > your parameter is blah
> > does work work now: blah
> > I'm here
> whow!! thank you. its seems to be a bit more complicated 
> than I thought. I would never have found the solution on my own!!
> 
> now I know for what reason we've got "send", "define_method",
> "instance_eval" and "const_set". can you explain me why "eval" was a
> thought in the wrong direction? 

Your code was

 def My_Module.subclass(name, &code)
  eval <<-EOS
    class #{name} < Bigclass
      def #{name}.run(p)
        super(p)
       #{code.call}
      end
    end
  EOS
 end

the basic problem is that you're opening a new scope when you do class X;
... end or def foo; ... end, so you cannot access the 'code' variable
directly; if it were possible to turn it into a literal (that is, if
it were a number, string, array, etc) you could use #{code.inspect}
to interpolate the value, but it's not possible in this case since it's
a Proc.  In order to be able to access code from the method definition,
you have to use a closure, i.e. define_method.

> (btw: the construct with k = Class.new... const_set(name, k) seems less
> elegant than eval("class #{name} < Bigclass..") at first sight)

I tend to avoid eval <<-EOF for a number of reasons:
 * syntax errors aren't caught at load time
 * the editor doesn't indent/highlight the code
 * (when defining classes/methods with class/def) no closures
 * interpolating values in the code feels cheap (and will only work for
   some classes)
 * it's eval :) using it is a bit like recognizing the language wasn't
   powerful enough to express what I wanted 'statically'

To me, eval'ing a text with lots of #{whatever} seems less elegant, for
the reasons stated above. I prefer one of the block forms (instance_eval{}
or module_eval{ }) or define_method when it makes sense.

-- 
Running Debian GNU/Linux Sid (unstable)
batsman dot geo at yahoo dot com

Why are there always boycotts?  Shouldn't there be girlcotts too?
	-- argon on #Linux