Metaprogramming has a place in Ruby but for the examples in the article there are far more readable ways to implement it.
Readability > cleverness every single time.
Metaprogramming has a place in Ruby but for the examples in the article there are far more readable ways to implement it.
Readability > cleverness every single time.
This is hacker news, not "production code" news. Abusing things in unexpected ways is pretty much the definition of hacking.
Many people seem to be jumping on this article as if the author was suggestion a new programming pattern we should start using, rather than an interesting look at some of the lesser-known quirks of ruby. I'm pretty sure even the author would agree that triple nested method definitions are not something we should use for production code.
What I would say is that although we should have an extremely good reason to employ these techniques in production, I believe that every professional Ruby programmer should be able to understand them and/or figure them out in a few minutes.
It’s not like we’re talking about obfuscated C.
However, metaprogramming is a power that should be used wisely. When implemented unnecessarily it reduces readability (and probably performance) for no real gain.
The devise source contains some great examples of metaprogramming used properly:
https://github.com/plataformatec/devise/blob/master/lib/devi...
Readability is in the eye of the beholder. For those unfamiliar with higher-order programming, maps are just a 'too clever' form of a for loop.
A clean and understandable solution is one matching the problem being solved in a precise way. In search for simplicity one can't forget that programming is a trade, and one should be expected to actually learn some shit.
In other words - just because you don't understand something doesn't mean it's "clever code".