Meta-Programming in Ruby – So Easy, So Elegant

Ruby is arguably the most meta-programming friendly language of the current dynamic languages since it has no compile time at all, and most constructs in Ruby are available at runtime.

Simply, there is no distinction between the code you are writing and the code that your computer runs, and you can change the code that your computer is running at any time you want.

Is it dangerous?

hell yes.

Does this prevent me doing it?

hell no.

I don’t think it is any different than AOP –regarding the complications–, it is also dangerous but we see it as a saviour most of the time. I think declarative transaction management is one of the best things happened in enterprise programming. Thanks to AOP and Spring.

I am not saying that just for meta-programming, it is the best fit for everything. There are times/projects that you should not use a dynamic language and should use a statically typed language like Java instead.

Just a couple of minutes ago, I created a controller in my RoR app to override a method in Net::Http to enable mocking. When enabled the controller replaces a method with another one that can read everything from file system instead of making HTTP calls. The best thing is, it just took me 10 minutes to figure it out and code it.

No need for point-cut expressions, just open Net::HTTP class.

No need for around advice, just unbind the method you want to wrap and bind it after wrapping.

READ  Source Control for your Code

Leave a Reply

Your email address will not be published. Required fields are marked *