From: Jeremy McAnally Date: 2008-02-25T14:29:24+09:00 Subject: Re: Monkeypatching is Destroying Ruby > > I don't think all language features are created equal. Some are more > dangerous than others. I think global variables are universally agreed to > be dangerous (in all languages), but most languages still support them > because they still can be useful. I believe monkey patching falls into the > same category. I think the point of this blog was to discourage people from > using it because it is dangerous. There are simple alternatives. > Any language feature can be dangerous. "OMG YOU CAN OVERWRITE FILES USING File.open()?? ITZ DESTROYING US!" As James said, you just have to know when, why, and how to use it properly. The idiom is a very powerful one if you use it with extreme caution. :) > > I don't see the big advantage of Array#to_csv over a more traditional > CSV::from_a(arr), other than a few more characters to type. It's not natural? You're probably returning a string but it looks like you're asking for a CSV? To make matters worse, performance (in my limited, completely contrived irb experiments) is slightly worse with the from_a method. > It is much more > encapsulated. The disadvantage of making Array#to_csv is that you are > modifying a global "variable" (the Array class). > That word doesn't mean what you think it means, I think. That isn't "more encapsulated". Even further, you're "modifying 'global variables'" any way you go. If you add a method to CSV, you're still adding a method somewhere, except now the API is awkward. So long as you document where the method is added (whether it's Array or your own CSV class), you're not hurting anyone. If people using your code hurt themselves because they don't read documentation, that's their fault. The only "dangerous" thing I see is perhaps namespace clashes, but in that case, it's the developer's responsibility to keep track of those. Even if you use your "encapsulated" version you could still have another CSV module/class from a from_a method that stomps on that one. It's a problem anywhere and not using monkeypatching just because you're afraid of that issue seems a little silly to me. Then again, maybe my systems (thankfully) haven't been "big" enough to really make this a concern. --Jeremy -- http://jeremymcanally.com/ http://entp.com Read my books: Ruby in Practice (http://manning.com/mcanally/) My free Ruby e-book (http://humblelittlerubybook.com/) Or, my blogs: http://mrneighborly.com http://rubyinpractice.com