On Jul 9, 2007, at 5:15 PM, John Carter wrote: > Just got the O'Reilly announcement of the book "Beautiful > Code"... here is the sample chapter by Tim Bray on Ruby! > > http://www.oreilly.com/catalog/9780596510046/chapter/ch04.pdf > > Pity he didn't run his examples by this forum first, I'm sure we could > have made them more beautiful.. Yep, mostly because for some reason I didn't know about sort_by. I wonder how I missed that? Obviously, the main point of my article was to encourage people to use the idiom of reading lines, picking 'em apart with a regex, and accumulating in a hash, invented in awk, brought to the world in perl, perfected in Ruby. sort_by aside, I'm not convinced that any of the alternatives to my main loop are more beautiful or readable. I personally think that the reason Ruby will enjoy an ever increasing market share in the programming-languages space is because it's more readable. I'm looking at the following... > EXAMPLE 4-2 . Printing article names > 1 ARGF.each_line do |line| > 2 if line =~ %r{GET /ongoing/When/\d\d\dx/(\d\d\d\d/\d\d/\d\d/ > [^ .]+) } > 3 puts $1 > 4 end > 5 end > > Since most Linux distro's roll the log files to a reasonable size I > would have just gone with... > > ARGF.read.scan( %r{GET /ongoing/When/\d\d\dx/(\d\d\d\d/\d\d/\d\d/ > [^ .]+) }) do |match| > puts $1 > end Really? Considering most of the readership is new to Ruby? > In Example 4.3 he has... > 1 counts = {} > 2 counts.default = 0 > > Personally I prefer... > counts = Hash.new(0) > or > counts = Hash.new{|hash,key| hash[key]=0} > buts that's personal preference I guess. Gosh, I think I totally disagree on readability grounds. -Tim