Is it possible to define an instance method in ruby from a string (the method name) and a block (the method's contents)?
I imagine this will need to use instance_eval(), but I haven't figured out how to mix the two data types yet. Both the string and the block are dynamically determined, so it would work to create the block with the "def #{string}" at the beginning - I don't know how to do this.
My use case is a class that represents a Bacula configuration file. The configuration file can have many different types of resources. They're all stored in a relatively complex data structure behind the scenes (for other reasons, simplifying this structure will not accomplish what I'm looking for). I'd like the resources to be rapidly accessible via named methods.
For example, A represents one config file, B represents another. A has resources Director, Client, Job and B has Messages and Director.
In this case, A should have methods director(), client(), and job() while B has messages() and director(). Each of these would return the relevant resource from the object's respective config file.
I know there are easier ways of doing this (like implementing a [] method), but at this point I'm pursuing the harder solution for curiosity's sake.