From: Eric Schwartz Date: 2006-03-16T07:28:47+09:00 Subject: Re: Ruby for Java Folks? "Wilson Bilkovich" writes: > How about working through some of the examples from Refactoring by > Martin Fowler, or Smalltalk Best Practice Patterns by Kent Beck, using > both Java and Ruby side-by-side? Most of my good books (like Refactoring) are packed up, alas! But I'll see if I can't come up with some simple examples of "Here's how you do it in Java, now here's Ruby". I want to avoid appearing to diss Java at all; these are presumably smart people who don't care to have advocacy thrown at them; I just want to give them info. > One thing I really like about Ruby is that certain tasks (like > enumeration) have much less code overhead than in Java, which promotes > nice, small methods that are easy to refactor or rework. Even if it's > only 6 lines, there's some mental resistance to setting up a loop in > Java. Yeah, I have to admit that, having done some C recently (had to interface with a new device driver, and didn't want to learn how to do ioctls in Ruby at the time), I hate doing C/Java-style for loops like this: String [] fileNames = new File( "/home/emschwar" ).list(); for(int i=0; i < fileNames.length; i++) { System.out.println("directory entry named [" + fileNames[i] + "]"); } There's no real reason for it, but it focuses my mind more on the array (how I set it up, how I'm iterating over it) when I'm reading it than in the Ruby below: Dir.entries('/home/emschwar').each { |e| puts "directory entry named [#{e}]" } Here, I'm more focused on what I'm doing to each element-- this reminds me a lot more of Perl's map (and Lisp's mapcar) function, but when I use Perl, I feel like I'm turning the iteration inside out; here it's just a much simpler way of doing the same for loop above. Without complaining about Java, showing how Ruby iteration eliminates the need for explicit iterators would certainly be powerful stuff. (personally, I prefer do...end, but used {} here to make Java people feel more comfortable :) -=Eric