This post originated from an RSS feed registered with Ruby Buzz
by Matt Williams.
Original Post: Classes on the Fly
Feed Title: Ruby Blender
Feed URL: http://feeds2.feedburner.com/RubyBlender
Feed Description: This blog contains short-ish ruby tips, hints, and techniques.
I recently had a case (a game) where I had the potential for many classes which were similar, but have enough differences it made sense to dry them up, so I developed a method to use yaml to define a class.
In my development of Machines, Monsters, and Madness, I've taken what might be described as a side excursion: I'm documenting the process of creating a simple wargame in Ruby which encompasses a library of game related utilities which will make its way into M3.
As it turns out, there are many unit and terrain types. These have a lot of overlap as well as there being many, many classes which would be needed. So, I've come up with a way to dry up my code and use a yaml file for generating these classes. With very little expansion, it could be used for a more generic solution (I'll get into that below).
In each of the classes, I am declaring an (optional) template, as well as (optional) attributes. The code which performs the loading of the yaml and the creation of the classes follows:
The method is a class method, so it's invoked as Loader.load_classes(file, Unit); if I choose not to pass in the second argument, then I need to specify classes in my yaml file for each class. I then load the yaml and for clarity split it into templates and classes. Then for each class, I do the following:
Output the class name; this could be suppressed if desired
Create a Class instance either using the parent argument or doing a lookup to find the parent and register it with Object.
Taking the class instance, I then add to it with class_eval and add the following:
Any additional traits which might be defined for the class
Merging the (optional) template's traits and the class' (optional) traits, I tranform them as follows:
Numerics are changed to "trait amount", as in "strength 10"
Strings are escaped to look like "symbol 's'"
There's definite places for expansion/improvement -- I could modify it so that a trait could be an array or a hash. also I could add the ability to embed code in the yaml file. But, all in all, it does save typing and makes my definition of units and terrains more flexible.
I'll gladly listen to any suggestions for improvement