December 14th, 2006
A very small Ruby method
I haven’t had much time or inclination for blogging lately. Not much happening at the moment, actually. But I came up with one small thing I wanted to document. Just a practical thing for certain situations. Basically it’s a with-method, that works fairly well. It’s nothing magical and the trick is basic. It’s more or less an alias, actually:
module Kernel def with(obj = nil, &block) (obj || self).instance_eval &block endend with("abc") do puts reverseend "abc".with do puts reverseend
As you can see, insstance_eval can be used like JavaScript or VB’s with. This is nice for the simple reason of documentation. I find this usage much easier to read and understand than most usages of instance_eval that I’ve seen.
So, that’s it for today. I’ll probably be back soon with some recent JRuby developments too.
