Hacker News new | ask | show | jobs
by mattjones 6682 days ago
To some degree, using open classes is just how you write functions in Ruby.

Say there exists a class called Errors in some Ruby library. It doesn't have an each() method, but I'd like it to. Nobody would complain if I wrote a function each_error() which took an Errors object as an argument. Reopening the Errors class and giving it an each() method is the way you do that in Ruby, and it's not really any more dangerous.

1 comments

Then lets say you packaged that up as a library. Another guy did the same thing, but it worked a little different. Now random code breaks.
It turns out not to be a problem in practice. I could list the 20 things I hate most about Ruby and the 20 things I hate most about Rails, and this wouldn't make either list. I don't know that I've ever seen it happen.

And on the "technically, could it happen?" side, sure, it could. You can also write a Java library that, in weird cases, could screw up other Java libraries, but the Java folks (even as much as they love to try to keep you safe) don't care, either.

Besides, "each" is quite standard in Ruby. Defining it to mean something other than "call the proc with each element, in order" would be weirdbad, like a C++ programmer defining operator+ to mean subtraction. If you do something weirdbad like that, your library deserves to lose.