A new method for making methods
The usual way of defining new methods is to put your intended logic between def
& end
. And that works perfectly fine for the most part.
But consider a situation where you have to create a series of methods all of which have the same basic structure save for one string, and which can only be one of a certain set of strings. Now, you might say we'll just define one method and accept that one string as a parameter, which we can then use wherever we want. And you'd be right. But, the problem with such an approach is it's not particularly declarative: you don't know exactly what values that optional parameter can accept.
The above piece of code causes three methods perform_rhinoplasty
, perform_checkup
, and perform_interpretive_dance
to be created. And invoking them calls the respective blocks that were passed to the define_method
call. This is clearly no better than just defining methods the old-fashioned way. In fact, it's a little bit worse since we've just added a bunch of extra words and strings and gained nothing for it.