I need to use Ruby to process performance data and calculate performance
metrics. What I have is a Hash that contains event_name => event_value
mappings. What I want to do is calculating metrics in a elegant style.
Thanks. it looks like a promising solution. but I have two more
requirements:
what if the event name includes a dot ‘.’?
I also want the left value of the equation (metric_A in the example)
to be added into the Hash, because it is possible that a new metric
needs to be calculated based on the existing metric.
Thanks. it looks like a promising solution. but I have two more
requirements:
what if the event name includes a dot ‘.’?
You have two event names and you need to clarify: the key in the hash,
or the “variable” used in the DSL?
If the former, you probably want to munge the names as they come in.
If the latter, you probably want to munge what you wind up instance
eval’ing.
If neither, you’re probably doing it as a scoping mechanism of some sort
and you probably need to build a hierarchy in @events and change method
missing to do a proper hierarchical lookup (probably recursively,
passing down to subsequent sub-EventData’s).
I also want the left value of the equation (metric_A in the example)
to be added into the Hash, because it is possible that a new metric
needs to be calculated based on the existing metric.
Then chuck the hash entirely and use local variables. This will probably
collide with the above suggestion for namespacing… so munging the
input script is a better route.
Can anybody give some hints?
If the only requirement is to have event_A evaluate to
events[“event_A”] you can do that easily with method_missing. This can
give you some ideas:
class EventData
def initialize events @events = events
end
def method_missing meth, *args, &blk
super unless /\Aevent_/ =~ meth.to_s @events.send(:[], meth.to_s)
end
end