def *tarea*_sum (num)
  tot = 0
  subtareas.each do |st|
    tot += eval("st.*tarea#{num}", binding , __FILE__, __LINE__)
*
  end
  return tot
end




2008/5/28 Lautaro Bonetto <[EMAIL PROTECTED]>:

> Hola a todos.
>
> Necesito hacerles una consulta.
> Tengo un sistema en Rails y necesito agregar a un módulos varios métodos
> que difieren entre sí sólo en un número. Quiero tratar de evitarme tener que
> escribirlos a todos.
> Existe alguna forma de "programar la programación"? Traté de hacerlo con
> EVAL, pero no me funcionó. Quizás lo hice mal.
>
> Ejemplo: Dos de los métodos son similares a estos:
>
> def *tarea0*_sum
>   tot = 0
>   subtareas.each do |st|
>     tot += st.*tarea0*
>   end
>   return tot
> end
>
>  def *tarea1*_sum
>   tot = 0
>   subtareas.each do |st|
>     tot += st.*tarea1*
>   end
>   return tot
> end
>
> Tengo que escribir esta función "tareaXX_sum" 20 veces, y lo único que
> varía es la parte de "tarea0" por "tarea1", "tarea2", "tarea3"... y así
> hasta "tarea20".
>
> Hay alguna forma de evitarme tipear todos los métodos en el modelo?
>
> Desde ya muchas gracias por la ayuda.
>
>
> --
> Lautaro Bonetto
> http://lautaro-bonetto.neurona.com
>
> _______________________________________________
> Ruby mailing list
> [email protected]
> http://lista.rubyargentina.com.ar/listinfo.cgi/ruby-rubyargentina.com.ar
>
>


-- 
Nicolas Alejandro Santa
Sr. Developer

ELC Technologies (TM)
Santa Barbara, CA 93102
[EMAIL PROTECTED]
[EMAIL PROTECTED]
(866)863-7365 phn
(866)893-1902 fax

http://www.elctech.com

+44 020 7504 1346 Tel - London Office
+44 020 7504 1347 Fax - London Office
_______________________________________________
Ruby mailing list
[email protected]
http://lista.rubyargentina.com.ar/listinfo.cgi/ruby-rubyargentina.com.ar

Responder a