From: Xavier Noria Date: 2008-01-29T03:15:56+09:00 Subject: Re: lining up delimiter separated text On Jan 28, 2008, at 6:44 PM, scooterm@hotmail.com wrote: > BEFORE: > fname ;; lname ;; age ;; sex ;; > homer ;; simpson ;; 33 ;; male > maggie;; simpson ;; 03 ;; female > bart;;simpson;;10;;male > > AFTER: > fname ;; lname ;; age ;; sex > homer ;; simpson ;; 33 ;; male > maggie ;; simpson ;; 03 ;; female > bart ;; simpson ;; 10 ;; male > > REQUEST: > Show a really cool way to change before into after with > ruby. Cool = fewest lines OR just plain clever. Modulus whitespace, here's a solution records = DATA.readlines.map {|line| line.chomp.split(/\s*;;\s*/)} max_widths = records.transpose.map {|col| col.map {|cell| cell.length}.max} records.each do |r| format = max_widths[0...r.size].map {|mw| "%-#{mw}s"}.join(" ;; ") puts format % r end __END__ fname ;; lname ;; age ;; sex ;; homer ;; simpson ;; 33 ;; male maggie;; simpson ;; 03 ;; female bart;;simpson;;10;;male