So, I’m not sure if Jay Phillips (of Adhearsion fame) is trying to annoy the purists with a remarkably Perlish hack in Ruby. I just know that his superators gem is a clever bit of pure Ruby code that allows you to apparently define new infix operators in Ruby programs. Imagine the joy of being able to write
a /~ b
and
rocket <=- fuel
You can with superators:
require 'rubygems'
require 'superators'
class String
superator "<=-" do |arg|
self << arg.upcase
end
end
Is this a good idea? I don’t know. I can see that it would have been useful in some DSLs that I’ve written. But I do think that it’s a wonderfully clever implementation (it doesn’t do anything nasty to the interpreter–it simply intercepts built-in operators (such as <<=
and -
) in my example above. And that’s what makes it great. Every now and then, just when you think you know Ruby. someone comes up with something—such as symbol.to_proc or superators—that makes you go “oh!.”
Every long-lasting relationship needs the occasional surprise to keep it interesting and fun. This kind of hack is one of the reasons I love Ruby.
No comments:
Post a Comment