help with eval

Hey, thanks for reading this :)
I have this little code, which logs the sent parameters(*args) whenever Window_something.new(*args) is called.
However, it doesn't seem to work- $window_parameters stays empty..
  1. class Window
  2.  
  3.   def self.inherited(subclass)
  4.     $window_parameters = {} if $window_parameters.nil?
  5.     code = "class #{subclass}" + "\n"
  6.     code +=  "alias win_init initialize" + "\n"
  7.     code +=  "def initialize(*args)" + "\n"
  8.     code +=    "$window_parameters[#{subclass}] = *args" + "\n"
  9.     code +=    "win_init(*args)" + "\n"
  10.     code +=  "end" + "\n"
  11.     code += "end" + "\n"
  12.    
  13.     eval(code)
  14.  
  15.   end
  16.  
  17. end
Expand to see the code.
try using eval("#{code}"), I know it's almost the same as eval(code) since code is a String, but I don't recall being able to use eval using a variable.
Thanks,
The main problem seems to be that I used eval rather than
class_name.class_eval(code)

I'm still getting this really weird error:
If I type the code manually for every class, it works perfectly.
But when doing so in class_eval, whenever I call initialize I get 'wrong number of arguments'. I've printed the sent arguments to test it and they're fine, but rmxp reads them as an array- so rather than 4 arguments it gets '1 argument for 4'.
I'm currently stuck, as I can't figure out how to turn the array back to a pointer/ list of arguments x.x

Here's the code
  1. class Window
  2.   # every sub-class of Window is saved in window_types.
  3.   def self.inherited(subclass)
  4.     $window_parameters = Hash.new if $window_parameters.nil?
  5.     $window_types = [] if $window_types.nil?
  6.     $window_types.push(subclass.to_s)
  7.   end
  8. end
  9.  
  10. #----------------------------------------------#
  11. # this bit goes above main,
  12. # it's called after all windows have been defined.
  13. #----------------------------------------------#
  14. def make_window_args
  15.  for winclass in $window_types
  16.     code =  "alias win_init initialize" + " ; "
  17.     code +=  "def initialize(*args)" + " ; "
  18.     code +=    "args = *args.clone" + " ; "
  19.     #code +=      "p args" + " ; "
  20.     code +=    "$window_parameters[self.class.name] = *args" + " ; "
  21.     code +=    "win_init(*args)" + "; "
  22.     code +=  "end"
  23.     (eval(winclass)).class_eval(code)
  24.     code = "def clone" + " ; "
  25.     code +=   "args = $window_parameters[self.class.name]" + " ; "
  26.     #code +=      "p args" + " ; "
  27.     code +=   "new = (self.class).new(args)" + " ; "
  28.     code +=   "return new" + " ; "
  29.     code += "end"
  30.  
  31.     (eval(winclass)).class_eval(code)
  32.    
  33.   end
  34. end
Expand to see the code.
I feel silly.. I only had to do
code += "new = (self.class).new(*args)" + " ; "

This is actually working now :)
I've been banging my head on the wall with this 'clone script' for a week now..
I can clone windows, next step- clone scenes, then profit :D
This is for my an auto-save/load script,which (hopefully) will be able to save in the middle of battle, etc.

Original thread: https://www.hbgames.org/forums/viewtopic.php?t=70954