Flexible way to provide formula variables

Formula variables is the term I use for any "special variables" that can be used in a formula.

I wrote a brief summary about it here

http://www.himeworks.com/2013/06/04/rgss-formula-variables/

Basically, it's things like in a damage formula, a is the user, b is the target, and v are the game variables.

Currently, all of my scripts hardcode the formula variables into method signatures, and it is impossible to add new variables without basically overwriting the method.

For example, consider the damage formula eval method

def eval(a, b, v=$game_variables) # logicendHow can it be written so that I can easily inject new parameters with default values?For example, I can overwrite the method as this

def eval(a, b, v=$game_variables, s=$game_switches, p=$game_party, t=$game_troop) # logicendBut if you wanted to add one yourself, you wouldn't be able to do it without overwriting my change.At this point, I'm thinking it is not possible given how methods and parameters work, but maybe there's some trick that ruby has that will allow us to accomplish this.
Why not just make methods in the same class as eval to reference this new data? Something like

def s; $game_switches; end should work the same as passing it as an instance variable.Would there be any reason to set it to a non-default value? That would be an issue with this method.
Hm.. maybe something like this:

module FormulaV  @data = { :var => $game_variables,  :switch => $game_switches}end # Somewhere elsedef eval(a, b, d=FormulaV::data)v=d[:var]# LogicendAdding new types of these variables would just mean adding to the hash. And possibly adding a line to read the variable out of the hash into a local variable.

I'm pretty sure there exist a meta-programming way to create local variables with a default-value, so that adding a line to eval could be avoided, but it has been a long time since i dabbled in the meta-programming of ruby.
Why not just make methods in the same class as eval to reference this new data? Something like

def s; $game_switches; endshould work the same as passing it as an instance variable.
Would there be any reason to set it to a non-default value? That would be an issue with this method.
I didn't think of approaching it as methods defined in the class, but it would work well.


When you put it that way, there are two types of formula variables


1. Ones that have constant references. eg: s always refers to $game_switches within the context of the particular eval method.


2. Ones that have variable references. eg: the calling method passes in an object such as the current battler


Defining methods would take care of #1 easily, and anyone can introduce their own methods without having to do much work: just throw in a definition and it's ready to go.


FOr #2, if someone actually wanted to pass in more arguments, something along the lines of what MagicMagor is going with may be required.

Original thread: https://forums.rpgmakerweb.com/threads/flexible-way-to-provide-formula-variables.24062/