Software engineer, data guy, Open Source enthusiast, New Hampshire resident, husband, father. Fan of guitars, hiking, photography, homebrewing, sarcasm.
Ruby API development: module/class structure to set instance variables via do/block
In this blog post I’ll show an example Ruby module/class structure to allow users to set configuration settings via a do/block, like:
The following code shows how a simple module/class structure could be defined to allow end-users to set instance variables on a module’s singleton class:
Usage and output:
Update 2013-10-20:
Below is another example that allows you to call methods on the singleton class, instead of using the assignment operator with a block parameter; as such: