I really enjoyed Jim Weirich’s session on polite programming at the Scottish Ruby Conference. He covered a problem that’s been vexing me for some time, about avoiding the use of method aliasing, by using inheritance instead. Unfortunately, his suggested solution didn’t tell me anything I hadn’t already tried. I still think this must be possible, but that I just don’t know quite enough about Ruby to be able to achieve it. Maybe you do?
Here’s the puzzle:
https://gist.github.com/912504
Can you solve it?
Here’s my attempt: https://gist.github.com/912526
Basically, if you use define_method, you can get closure-like behaviour around the method body, since this is defined as a block.
I suspect this will only work in Ruby 1.9.2 because of some of the restrictions for defining block arguments in earlier versions of Ruby.
I’m not 100% sure about this solution, but it certainly passes your assertion at the bottom.
Here’s my revision: https://gist.github.com/1087802
I make it into shorter by using definesingletonmethod.