3 ms·
I'd almost blame this problem on Ruby not having its own standard Array#sum method (or more likely it should be a method on Enumerable?). I'm guessing a lot of
by Locke 18y ago
I'd almost blame this problem on Ruby not having its own standard Array#sum method (or more likely it should be a method on Enumerable?). I'm guessing a lot of projects add their own, so it's a natural source of conflict.
Secondly, it seems like most conflicts like this occur between Rails and some other library. Rails, via ActiveSupport, adds a lot of extensions to core Ruby. I kind of wish it didn't. Framework or general purpose library developers should recognize that their library will likely be combined with many others and avoid conflicts by sticking to what's available in the standard, namespacing their code, etc.
I like Ruby, but I'll readily admit it's not a perfect language. I'm sure someday a better Ruby-like language will come along. Hopefully, it'll be a language with better support for avoiding these kind of conflicts without removing support for open classes. I'm sure it's an interesting technical challenge.
- carbon8 18y agoRails, via ActiveSupport, adds a lot of extensions to core Ruby. I kind of wish it didn't. Apparently modular ActiveSupport is in the works: http://github.com/wycats/rails/commit/21c17e893fd8f5f7dc41df3bdb9e39a0b30e5a36 http://github.com/wycats/rails/commit/21c17e893fd8f5f7dc41df...
- warfangle 18y agoI'd blame this problem on gem writers monkeypatching base functionality. Why not use the Array.map and pass it a summing block? That way if you've got an array of things other than numbers, you can correctly utilize the strategy pattern to sum them....
- raganwald 18y agoWhile I grouse about Rails, here is the challenge I posed last July at Rubyfringe: Forget about Rails providing all these extensions for Rails programmers to use. How does Rails perform this kind of metaprogramming for its own use? How can its own internal libraries use Array#sum while leaving Array unmodified for Rails programmers?
- Locke 18y agoIn my own library, I use inject instead of adding Array#sum. But, I know that's sidestepping the issue, not solving it. Honestly, I'm not sure how Rails uses #sum. I'm not sure whether they really need it or not. I suspect not. I'm not a big fan of a lot of non-standard additions to Ruby. For example, all the #try, or #returning, or even your #andand. I don't consider any of those to have enough intrinsic value to be worth the risk of collisions with someone else's subtly different implementation.
- raganwald 18y agoI'm not a big fan of a lot of non-standard additions to Ruby. For example, all the #try, or #returning, or even your #andand. I don't consider any of those to have enough intrinsic value to be worth the risk of collisions with someone else's subtly different implementation. #andand is soooooooo "raganwald." The Homoiconic Way is to use rewrite: #try, #returning, and #andand are all removed from your code and never conflict with other people's implementations ;-)
- leadnose 18y agoI don't quite understand why that sort of methods should belong to array-class. Should array also have product-method? Average? Standard-deviation? Concatenating strings? Where should you draw the line?