From: Gavin Sinclair Date: 2003-01-22T11:25:44+09:00 Subject: Re: Ruby idiom (0...(expr)).map On Wednesday, January 22, 2003, 4:17:08 AM, Martin wrote: > More usefully, Range#map is a shortcut for Range.to_a.map, but since I > most commonly want to start from 0, I thought a further shortcut for > (0...n).map would make my code prettier. > Frinstance, here's a snippet from some code I'm currently writing to > format text into columns: > npages.times {|i| ranges.push((pagebreak * i)..(pagebreak*i+psize-1))} > ranges.each {|r| > page << r.map {|line| > (0...ncols).map {|col| text[line + col*psize]}.join(col_sep) > } > } > where I want an array each element of which is a function of the current > index. Here it's not so bad, since I've already had occasion to define > ncols, but if it were an inline expression, the code would start looking > really messy. Well, you always have "occasion" to define the "expr" in (0...expr). If it's inline and messy, you create an expression variable. I can't see that particular idiom disappearing in a hurry. Have you tried the shortcut yourself, with pleasant results? class Fixnum def map(&block) (0...self).map &block end end Gavin