Just yesterday I started the what I learned today series and I'm already thinking it should have been a weekly digest. Not that I didn't learn anything new (I, in fact, did learn a few things that I will share in a second), I just figured it would be too much noise to publish a digest daily. So weekly it is.
Today I instead want to talk about metaprogramming in Ruby. I actually not so much want to talk about it as I want to put a summary for my future self. Like a collection of links that finally made me get it. The links are in no particular order:
- A great episode of the Ruby Rouges podcast about what metaprogramming is, how it is different from monkey patching, when and why and how you would do it, and beyond. Great stuff. I would actually highly recommend this podcast even to folks not doing Ruby coding on a daily basis (I am not, for example). I have recently picked it up and my commute just got so much better. Haven't yet caught up on all of the episodes.
- Great post from Yehuda Katz about how it's all about self
- This question on SO and the answer to it upvoted 66 times
- Alternatives for redefining methods with pros and cons
- define_method, method_missing, and instance_eval with examples of how Chef DSL is made
I am sure there are more decent blog posts out there on what metaprogramming in Ruby is about and how to do it right and when it is right to do it so please add it to my collection.
UPDATE. As I was wrapping up this one I came across two more very interesting and very relevant posts:
- awesome writeup on include and included. It goes into showing off bizarre examples of massive metaprogramming in ActiveRecord as well as some conscious evolution of rails in doing it more right than it was doing it before
- A response to the post I just mentioned that brings up an interesting point of namespace pollution. While I am ok protecting the global namespace in JavaScript with the "closure everywhere" (function(...){})(); pattern, I am not sure I see the point in giving up syntax convenience and protecting my class namespace in the same way.
No comments:
Post a Comment