From: David Masover Date: 2010-12-24T05:32:04+09:00 Subject: Re: Ruby and science ? On Saturday, December 18, 2010 11:27:48 am Charles Oliver Nutter wrote: > > About the only unintuitive thing I ever found was implementing a Java > > > interface, and while it's somewhat unintuitive, it's still trivial: > If you have suggestions for how to improve it, we'd love to hear them :) Not really. Put another way: Most Java constructs translate intuitively to Ruby, as soon as I just conceptually gave up on even trying to use generics, but Java can work with raw types, too, so that's not that big a leap. Nearly everything I want to do with JRuby and Java, I don't have to go to the docs, it just does what I'd expect: require 'java' import java.util.AbstractCollection import java.util.ArrayList class MyCollection < AbstractCollection def initialize super @coll = ArrayList.new @coll << 1 @coll << 2 @coll << 3 end def iterator @coll.iterator end def size @coll.size end end Not only does that give me the stuff I wanted from AbstractCollection, but Java "knows" it's a working collection, so I can do this: import java.util.Collections c = Collections.unmodifiableCollection MyCollection.new i = c.iterator i.next # returns 1 i.remove # throws java.lang.UnsupportedOperationException Basically, by knowing Java and Ruby, and looking at the Javadoc for various Collections-related stuff, I was able to write the above without knowing a single thing about JRuby other than that I have to "require 'java'" first. So maybe it's just me, but the first time I actually had to Google for how JRuby does something was when it wasn't immediately obvious that I needed: > > include Comparator That's about the closest you could map that onto Ruby, but unlike the previous, it's not a perfect match, because Ruby doesn't have interfaces, or really anything like them. In Ruby, if I'm going to completely implement Comparator, there's no reason I should ever have to include Comparator. I don't really see a good way around that, though. Plus, this stuff is cool: > Or this may work too (I don't remember PriorityQueue's API): > > pq = PriorityQueue.new(11) do |a, b| > a.to_s <=> b.to_s > end That does work. So this isn't a complaint, really. How could I complain when actually learning about JRuby gives me even easier interfaces than I'd expect? > Oracle's actions relating to Java have all been political. At the same > time people publish that they're fighting with Apache or Google, they > are also getting IBM (GPL-haters) and Apple (not big OSS contributors) > to collaborate on the GPLed OpenJDK, and making concrete plans for > OpenJDK to continue beyond Java 8. ...at the same time as they're also making things difficult for anyone who wants to start an implementation of Java other than OpenJDK. The fact that Ruby has MRI (1.8 and 1.9), JRuby, MacRuby, and others is a _strength_. There really only is the one implementation of Java. The fact that OpenJDK exists and is GPL'd helps me sleep better at night, but the fact that Oracle is suing people over patents makes me wonder how useful the GPL actually is in this case. It's kind of like x264 being GPL'd -- doesn't make H.264 any more open. > As far as *using* Java, nothing has changed for the worse in the past year. Right, and I am using it (and JRuby). It's just the long-term stuff that scares me. I suppose it's not actually a change for the worse given Sun always had the ability to do the stuff Oracle's doing now, but every now and then, it looks as though Oracle might actually be clueless (or evil?) enough to run Java into the ground for a short-term profit. I doubt it's going to happen, but I do try to keep enough of my code portable that a switch back to MRI would be possible, if incredibly painful.