From: "Kroeger, Simon (ext)" Date: 2006-05-22T23:56:14+09:00 Subject: Re: rubynuby - confused by method name "inject" > ..though the other uses of inject defy "inject_operator"... > > data = [[1, 7, 3], [1, 2], [1, 5, 4, 1], [1, 8]] > > longest_sample = nil > longest_length = data.inject(0) do |accum, sample| > if accum > sample.length > accum > else > longest_sample = sample > sample.length > end > end > > puts "Longest sample: #{longest_sample.inspect}, length > #{longest_length}" > > ..then again, using inject to find longest seems a bit silly... Well, Enumerable#max is the way to go here, but inject isn't that clumsy too: longest = data.inject{|max, this| this.length > max.length ? this : max} puts "Longest sample: #{longest.inspect}, length #{longest.length}" I love it, it's the swiss army knife of Enumerable. cheers Simon