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"); } });

14 Responses to “Ruby vs Java”

  1. Behrang Says:
    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.
  2. Ben Says:
    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.
  3. Ben Says:
    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.
  4. Behrang Says:
    One more thing :-) Unicode is not a first-class citizen in Ruby...
  5. carlos orrego Says:
    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
  6. TimothyP Says:
    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)
  7. Tom Klaasen Says:
    @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.
  8. TimothyP Says:
    @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.
  9. TimothyP Says:
    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.
  10. Tom Klaasen Says:
    @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.
  11. Nicolas Says:
    [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!
  12. bob Says:
    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.
  13. Behrang Says:
    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.
  14. ahmet Says:
    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.

Leave a Reply