Ruby vs Java
February 18th, 2008
addresses.find {|address| address.street == 'Groenplaats'}vs.
CollectionUtils.find(addresses, new Predicate() { public boolean evaluate(Object obj) { Address address = (Address) obj; return address.getStreet().equals("Groenplaats"); } });

April 21st, 2008 at 10:20 PM Tom, This is the Hibernate oneliner I was referring to: List addresses = session.createQuery("from Address as add where add.street = ‘Groenplaats’").list(); Also, when a Java app crashes a few times a day, it is usually because that the app itself is written poorly. But Ruby apps crash a few times a day because the Ruby runtime environment is not mature enough yet.
April 21st, 2008 at 10:20 PM There is a framework for java yet to be released (still working on open sourcing it) that will allow this: for( Address a : addresses.find(addresses.street.equals("Groenplaats")) ){ // do something } Not only that, but the following is legal: for( Address a : addresses.find(addresses.street.equals("Groenplaats").or(addresses.streetNumber.lessThan(12) ){ // do something } We don't know how the community will respond, but we think it is pretty powerful.
April 21st, 2008 at 10:20 PM By the way, its also completely type safe. Often a lot of the other projects don't allow type safety. This means you get code completion for your sql queries in plain old java.
April 21st, 2008 at 10:20 PM One more thing :-) Unicode is not a first-class citizen in Ruby...
April 21st, 2008 at 10:20 PM you can always use groovy on the jvm and hava one liners like this to query the database. I have use RoR for a big project, we had to go back to java, unstable, creashes a lot. Not suitable for big projects. IMHO
April 21st, 2008 at 10:20 PM C# var result = addresses.Where(x => x.street == 'Groenplaats'); Or you could use the SQL like syntax, but I'm not a big fan. (This sample isn't c# specific of course)
April 21st, 2008 at 10:20 PM @TimothyP: Cool, I didn't know that. I still have that "C# for Java Programmers" book lying around somewhere. Maybe it's time to give it another look.
April 21st, 2008 at 10:20 PM @Ben: I think they'll like it. It looks a lot like LINQ and type safety is very important (At least I think so). @Tom: This kind of functionality is available for most .NET languages through LINQ.
April 21st, 2008 at 10:20 PM Hey, Don't mean to bloat your comments, but I forgot to mention this. I wrote a short article on LINQ a while ago: http://www.itcrowd.be/entry/Smaller+and+faster+code+with+LINQ.aspx It shows you the basics.
April 21st, 2008 at 10:20 PM @Behrang I'm pretty sure that there are also Java projects that crash 10 times a day. And I won't go into a flame war about Hibernate vs ActiveRecord. I'm only challenging you to find a one-line Java equivalent for the problem above.
April 21st, 2008 at 10:20 PM [a for a in addresses if a.street is 'Groenplaats'] of (iets meer 'old school') filter(lambda s: s.street is 'Groenplaats, addresses) Python FTW!
April 21st, 2008 at 10:20 PM How about the xpath version which finds any address node in the whole object graph with the proper street attribute: //address[ @street = 'Groenplaats'] Strong typing is pretty damned cool when you decide you want to make your address base class just contain gps coordinates and have a StreetAddress sub-class contain the getStreet() method. You can just get rid of the getStreet() method in the address base class and move it to the StreetAddress sub-class and this piece of code will break at compile time instead of not finding the address at run-time even though it's in the DB. I know that's a bit of a screwy example buy similar kinds of things happen in big systems that are maintained for long periods of time.
April 21st, 2008 at 10:20 PM In RoR, your model classes extend ActiveRecord. In Hibernate, JPA, and similar frameworks they do not. However, if you use a Java framework in which model classes extend a base class, then you can have similar queries. Besides that, if you use Hibernate, for example, you can use HQL and your Java query that spans 6 lines becomes a 1-liner. Besides that, some RoR apps used to crash 400 times a day and still crash about 10 times a day* ;-) No matter how beautiful the Ruby language is, its runtime environment is very unreliable and unstable. * http://www.zedshaw.com/rants/rails_is_a_ghetto.html.
April 21st, 2008 at 10:20 PM actually if you use google collections predicate, you don't need the casting. Posibly with Java 7, this will have a new way of writing. Lastly , i can bet, java version will run around 20 times faster then ruby (or groovy) one.