Ruby 2.0 Refinements in Practice
yehudakatz.com
yehudakatz.com
http://groups.google.com/group/ruby-core-google/msg/7ccc3759...
JRuby seems rather important these days. I wonder if that gives the project an effective veto on (bad) language features?
It's taken a long time to get Ruby's performance up to the respectable levels it finally achieves with 1.9 and JRuby, so I'm glad to see Charles pushing to avoid letting features creep in which could seriously degrade it.
http://shootout.alioth.debian.org/u32/which-programming-lang...
JRuby and Rubinius have been working to improve Ruby performance. In theory, it might possible for Ruby to run at 1/2th to 1/10th the speed of C using an aggressive tracing and optimizing compiler similar to LuaJIT.
But this proposed feature really makes me cringe: If refinements require frequently invalidating method caches, Ruby performance will always be horrible. I'd rather sacrifice a tiny amount of expressiveness and have my code run 50x faster, thank you. :-)
EDIT: Note that there are some very cheap ways to implement a similar feature, including the way Common LISP supports package-scoped symbols.
I also really sympathize with the notion of sacrificing some degree of expressiveness for performance. If this feature is to be added, it needs to be done in such a way that it does not set back the hard-earned performance improvements of the last few years.
Then again, expressiveness is Ruby's niche, and DHH's famous quote about "sacrificing CPU cycles in favor of developer cycles" is quite valid, IMHO. If you need raw performance you should look to another language because I don't think that will ever be too high a priority for Ruby.
Github uses Node and (IIRC) Erlang for some stuff, and Twitter uses Scala - if you're lucky enough to have the kinds of problems those guys have, then the best bet is usually to use Ruby where it gets you a developer speedup and another language for the parts of your app where performance is absolutely critical.
That said, even though 1.8 is slow as a dog, often, it's _fast enough_. Your last paragraph hits it on the head.
Use RVM, it's a small bit of overhead that greatly increases productivity and reduces headaches.
The next programming language I use full time will have to have all the RVM features and utilities built into their RubyGem like utility, or I'm not even going to bother.
I reject this philosophy: Lua is also a highly expressive programming language, and LuaJIT runs 75 to 100 times faster than Ruby 1.8. There's no reason why the Ruby community should settle for lousy performance.
I'm trying to track down a bug in the Rubinius JIT as I speak. Rubinius may be faster than 1.9, but I can still see dozens of really obvious optimization opportunities. If nobody screws up method caching, Ruby will eventually be 10 times faster than it is today.
I'm not a Ruby internals expert, but I've done my fair share of meta programming and debugging of monkey-patch conflicts. A more static version of refinements will give 99% of the benefit while avoiding most of the landmines Charles is describing. The other 1% can probably be served by adopting some other technique rather than looking to refinements. Ruby is already such a dynamic and powerful language that it seems silly to say that refinements need to have this uber-flexibility. All they need to be able to do is isolate monkey patches within a library and they've already provided a huge practical benefit. Being able to inject refinements into someone else's code? That's just not very important, and if it is for some particular case there are other techniques Ruby affords you, all of which should be used cautiously.
Barring some flaw in Charles' analysis, I'd say it's a no brainer that refinements need to go back to the drawing board.
Perhaps they'll change that in 2.0 as well.
Nice to see it finally being implemented in the core language.
This will break most of the code out there, but in light of doing it cleanly, it would probably need to be done. On the other hand, a change like that will make sure that we might never see significant adoption of Ruby 2
I'd imagine that some of the reason is due to the fact that this is already implemented, but classboxes are not. I don't know anything about ruby-core, so I don't know how much that'd matter to them, but an almost identical but implemented feature seems much better than a more pure but unimplemented one.
... but YKatz's article gives me comfort that I made the right decision. While you can accomplish many of the same bits of magic in Python, it's much harder to do so. Clearly, I'm not a Ruby ninja, but I am someone trying to get stuff done and the pervasiveness of monkey-patching in Rails bit me a number of times (track down some function only to find out that I was looking at the wrong, un-monkey-patched version). I hadn't really realized how much I don't miss Ruby until reading through YKatz's article on how Ruby 2.0 will handle monkey-patching. How about just making monkey-patching harder?
Python has plenty of complexity, but you don't need to know about it: you can use Django just fine without paying much attention to the richness of Python. Ruby also has plenty of complexity and you need to know about it: use Rails or add a plugin to Rails and you'll quickly be reading through complicated Ruby classes that monkey-patch Rails.