Ruby is the language for coding brevity with elegance. The more you “get” Ruby, the less code you end up writing. One of the ways you do this is to just metacode the things you find yourself doing a lot and become more productive. And that’s a beautiful thing!
Recently, I’ve found myself writing and rewriting class attribute accessors, like
Being from the old school, while I’m not adverse to just using the @@
directly inside the class, I prefer some encapsulation.
And when you want to expose the class’ innards, you need to write these methods anyway.
This if fine, but it gets a bit too verbose for me.
Instance attributes are exposed easily with the attr
methods
which creates the barfle
and barfle=
instance methods.
What I want is an analogous
to create my self.bar
and self.bar=
methods.
Metaprogramming to the rescue!
Sweetness. I just love Ruby!