Re: [jug-discussion] Groovy and JRuby
At 09:42 PM 6/7/2008, Chad wrote: The challenge is "show me all the operations you can perform on an Array". Here's how you do it in (J)Ruby: chadmac:~ woolley$ jruby --command irb irb> [].class => Array irb> [].methods => ["frozen?", "sort", ...] irb> [].methods.sort => ["&", "*", "+", "-", "<<", "<=>", "==", "===", "=~", ...] # almost, but still has the methods from Object irb> [].methods.sort - Object.methods => ["&", "*", "+", "-", "<<", "[]", "[]=", "all?", "any?", ...] # Ah, just the methods from Array Now, I tried to do this same thing in Groovy (with groovysh), and I failed frustratingly and miserably. Really? It's pretty similar in Groovy: groovy> [].class.methods*.name.toList() ===> [get, add, add, indexOf, clone, clear, lastIndexOf, contains, addAll, addAll, size, toArray, toArray, remove, remove, isEmpty, set, trimToSize, ensureCapacity, hashCode, equals, iterator, subList, listIterator, listIterator, toString, containsAll, removeAll, retainAll, getClass, wait, wait, wait, notify, notifyAll] groovy> Object.class.methods*.name.toList() ===> [hashCode, getClass, equals, toString, wait, wait, wait, notify, notifyAll] groovy> [].class.methods*.name.toList() - Object.class.methods*.name.toList() ===> [get, add, add, indexOf, clone, clear, lastIndexOf, contains, addAll, addAll, size, toArray, toArray, remove, remove, isEmpty, set, trimToSize, ensureCapacity, iterator, subList, listIterator, listIterator, containsAll, removeAll, retainAll] And, notice that since I'm building on top of Java, I may not have had to explicitly filter out the Object methods (depending on the problem). I might have just been able to call java.lang.Class.getDeclaredMethods: groovy> [].class.declaredMethods*.name ===> [get, add, add, indexOf, clone, clear, lastIndexOf, contains, addAll, addAll, size, toArray, toArray, remove, remove, writeObject, isEmpty, readObject, set, trimToSize, ensureCapacity, removeRange, fastRemove, RangeCheck] And, of course, I could have sorted any of these with one more call: groovy> [].class.methods*.name.toList().sort() ===> [add, add, addAll, addAll, clear, clone, contains, containsAll, ensureCapacity, equals, get, getClass, hashCode, indexOf, isEmpty, iterator, lastIndexOf, listIterator, listIterator, notify, notifyAll, remove, remove, removeAll, retainAll, set, size, subList, toArray, toArray, toString, trimToSize, wait, wait, wait] To partially answer your original question: I use Groovy because it is built on top of a mature and immense language platform, so I don't have to reinvent the wheel every time I sit down to code. regards, -tom
Re: [jug-discussion] Groovy and JRuby (was Re: [jug-discussion] next month's meeting)
On Fri, Jun 6, 2008 at 4:14 PM, William H. Mitchell <[EMAIL PROTECTED]> wrote: > As I've mentioned before, I liked Groovy from a distance but I found it to > be frustrating to use. My experience with Ruby was the opposite -- blah at > first, but I quickly came to love it. Thanks William. I was waiting for someone who agreed with me to chime in before I responded on the thread. I've been using Ruby full-time, professionally for over two years now, and a hobbyist for a while before that. I used Java for several years before that, some of them with Scott and crew. To sum it up, Ruby makes me happy. I enjoy programming Ruby more than any other language. Many other people who work with Ruby say the same thing. This is not surprising, because Matz had this goal in mind when he created Ruby. Here's some quotes from him: "Does the world need another language? In theory, no. We just need the Turing machine to solve all of our problems, in theory. Humans require more sophisticated tools to program. It's a matter of human need. As long as some people feel happy using Ruby, that's enough of a reason for another language for me." [1] ""For me the purpose of life is partly to have joy. Programmers often feel joy when they can concentrate on the creative side of programming, So Ruby is designed to make programmers happy." [2] So, philosophy is fine, but lets see some code to prove this example. Here's an example. Say I'm on a plane, and I am playing with (J)Ruby and Groovy to compare them (which I was). No interenets or reference books, just the interactive interpreters. The challenge is "show me all the operations you can perform on an Array". Here's how you do it in (J)Ruby: chadmac:~ woolley$ jruby --command irb irb> [].class => Array irb> [].methods => ["frozen?", "sort", ...] irb> [].methods.sort => ["&", "*", "+", "-", "<<", "<=>", "==", "===", "=~", ...] # almost, but still has the methods from Object irb> [].methods.sort - Object.methods => ["&", "*", "+", "-", "<<", "[]", "[]=", "all?", "any?", ...] # Ah, just the methods from Array This is a beautiful experience which follows the Principle Of Least Surprise. For example: * Everything just prints out through the magic of duck typing, no class cast exceptions * [] IS an instance of an array * If I want the class or methods of this Array instance, I just ask it * If I want to sort the resulting array, I just call #sort * The "-" (subtract) operator does the logical thing, which is subtract the elements of one array from another. So, [].methods.sort - Object.methods gives me just the methods for Arrays Now, I tried to do this same thing in Groovy (with groovysh), and I failed frustratingly and miserably. I could get to the point of println'ing the methods of an Array (which is really an ArrayList in lipstick), but could see no way to do the nifty array subtraction to get rid of Object's methods. Not to mention the numerous exceptions when groovysh evaluates something that is not a string, and I have to println to get anything shown. Overall, confusion and NOT happiness. As a challenge, why don't the Groovy fans attempt this same thing and post it? I'll wager you write a lot more (ugly) code, and as William says, this is because Groovy is just too close to Java. Now for the unsubstantiated uninformed rant (couldn't get by without one of those): As for Grails, why use a substandard Rails clone built on a substandard Ruby clone? Check out Merb, that's the latest shiznit in Web Frameworks, and it is pretty sweet: http://merbivore.com/ It is similar to Rails, except they are doing all the things right that Rails does wrong. And for the ORM fans, check out DataMapper from the same crew: http://datamapper.org/ Sorry I'll miss the meeting, but I'll make it to another one soon to wax pedantic and give you Java guys grief ;) -- Chad [1] http://www.linuxdevcenter.com/pub/a/linux/2001/11/29/ruby.html [2] http://www.artima.com/intv/rubyP.html - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]