hacking rubys syntax
What?
In Ruby you have basically two ways of defining private methods:
I see a small problem with both approaches. In the first one, and the most obvious, is that you need to duplicate the method name as well as add an extra method call - private - just to change its visibility.
The second approach avoids this but adds the risk of accidentally putting a method that is intended to be public under the private section of the source file, which can render an annoying debugging session.
Why?
Personally, I like to have a smooth reading flow in my source files. That means that if the public method_a makes use of the private method_b, I want method_b defined right below its caller, which is possible - but verbose - using the private method call:
But can be somewhat harder to accomplish if you decide to split your source file in sections:
I wanted to be able to define a private method with a single reserved keyword...
How?
What if I could define a private method using this new syntax:
It turns out I can.
Notice the def_p keyword? This is a new keyword I created by changing ruby's parser and that behaves mostly like the def keyword, except that it defines a private method instead.
If you wanna read the code that allows this behavior and try it yourself, download the patch I wrote and apply it to the ruby source code - I patched version 1.9.1-p376.
After applying the patch, just build ruby as usual:
And then try running this script:
You should see the following output:
Happy hacking :)