monkeypatching ruby
As part of (re)-learning Ruby, I’ve been reading “Eloquent Ruby” and finally got to the chapter about “monkey patching”.
Some context: in Ruby, you can simply “re-open” classes willy-nilly. As below:
class String
def is_string; true; end
end
"".is_string
Just an ordinary day in Ruby land.
Perhaps more interesting is the fact that you can “alias” methods. So, if you’re more familiar with hd and tl terminology, you can do this:
class Array
alias hd first
alias tl last
end
[1, 2, 3].hd # 1
Also of note is the fact that aliasing a method allows us to “save” it. Which, I suppose, can be useful if you’re scared that your change will break something…
class String
alias add +
def +(other)
add(other.upcase)
end
end
puts "hello" + "world" # helloWORLD
Mmm. Definitely interesting.
Although I’ve been enjoying Ruby so far, I don’t know how I’d feel about having to deal with monkey patching in a real code base. A Rails core team member thinks you shouldn’t do it, and they probably have damn good reasons for saying so.
Anyway, that’s about it. Will start on “Metaprogramming Ruby” soon - maybe there’ll be something to write about.